当前位置:网站首页>Common methods of object class
Common methods of object class
2022-06-25 23:52:00 【Program diary】
Object Class common methods
equals()
When I saw this, I first thought ,equals and == difference
equals and ==
==:
For basic types ,== It determines whether the two values are equal
For reference types ,== It determines whether the two refer to the same object .
equals:
The basic type does not equals Method .
For reference types , If equals Method is not overridden , Then the function and == identical , It is used to judge whether the same object is referenced .
If equals Method is overridden , Whether the content of the comparison object is the same .
Be careful : rewrite equals Method must override hashCode Method , If you do not override hashCode Method , It's the emergence of equals Judgment for true But there are two objects hashCode inequality
Test code :
import java.util.Objects; class Solution { Integer a ; @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Solution solution = (Solution) o; return Objects.equals(a, solution.a); } @Override public int hashCode() { return Objects.hash(a); } public static void main(String[] args) { Solution solution = new Solution(); solution.a = 10; Solution solution1 = new Solution(); solution1.a = 10; System.out.println(solution == solution1); System.out.println(solution.equals(solution1)); System.out.println(solution.hashCode() == solution1.hashCode()); } }
hashCode()
Return hash value ,equals Method to determine whether two objects are equivalent .
equals identical ,hashCode It must be the same ,hashCode identical , however equals Not necessarily the same , Because computing the hash value is random , Two different objects may calculate the same value .
toString()
Default return : Class name @ Hexadecimal number , This hexadecimal number is the unsigned hexadecimal number of the hash value , You can rewrite to adjust the output .
for example :
Solution@1b6d3586
clone()
Is to clone objects , The default cloning method ( Override to call the parent method ) Is a shallow copy , Can be rewritten to implement deep copy ( In rewriting clone Method to create a new object for copying ).
clone Methods use protected Method modification , Cannot call without rewriting , Overridden classes must also implement Cloneable Interface , It means that my class can clone, If it is not implemented, an exception will be reported CloneNotSupportedException.
Shallow copy and deep copy
Both for basic types , All copy values .
For reference types :
Shallow copy : Copy the reference address , The new copy still points to the same object .
Deep copy : Create new objects , Copy content to new object .
clone() Not recommended , Both complex and risky , And throw an exception , Type conversion required .
You can use the copy constructor to implement .
public class CloneConstructorExample {
private int[] arr;
public CloneConstructorExample() {
arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = i;
}
}
public CloneConstructorExample(CloneConstructorExample original) {
arr = new int[original.arr.length];
for (int i = 0; i < original.arr.length; i++) {
arr[i] = original.arr[i];
}
}
public void set(int index, int value) {
arr[index] = value;
}
public int get(int index) {
return arr[index];
}
}
边栏推荐
- 史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
- mysql集群
- Jenkins 发布PHP项目代码
- WordPress
- SSL/TLS、对称加密和非对称加密和TLSv1.3
- Understanding of pseudo classes
- Talk about the copy on write mechanism of PHP variables or parameters
- Mutual conversion of float type data and datetime type data in sqlserver2008
- js实现输入开始时间和结束时间,输出其中包含多少个季,并且把对应年月打印出来
- Doris 运维中遇到的问题
猜你喜欢
![搜索旋转数组II[抽象二分练习]](/img/db/3ea01cf1ad8446a7007891ef1d8e7f.png)
搜索旋转数组II[抽象二分练习]

支付宝支付接口沙箱环境测试以及整合到一个ssm电商项目中

产品经理如何把控产品开发的进度
![mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动](/img/b2/2b87b3cea1422e2a860f5e0e7dcc40.png)
mysql5.7版本在配置文件my.ini[mysqld]加上skip-grant-tables后无法启动

Blob

C# IO Stream 流(二)扩展类_封装器

如何进行流程创新,以最经济的方式提升产品体验?

STL教程5-STL基本概念及String和vector使用

The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high

How does excel translate Chinese words into English automatically? This formula teaches you
随机推荐
剑指 Offer 48. 最长不含重复字符的子字符串
InputStream流已经关闭了,但是依旧无法delete文件或者文件夹,提示被JVM占用等
.user.ini文件导致的php网站安装问题
Apache doris1.0 cluster setup, load balancing and parameter tuning
录屏转gif的好用小工具ScreenToGif,免费又好用!
Talk about the copy on write mechanism of PHP variables or parameters
1.8 billion pixel Mars panorama Ultra HD released by NASA, very shocking
Tensorflow中CSV文件数据读取
对象数组去重
Format the number. If the number is not enough, fill in 0, for example, 1:0001,25:0025
4个要点,助力产品管好项目
利用swiper实现轮播图
How to generate get/set methods in idea
Doris 运维中遇到的问题
数组常用的一些操作方法
解析产品开发失败的5个根本原因
先序线索二叉树
QT Chinese and English use different fonts respectively
OpenResty篇01-入门简介和安装配置
Linking MySQL database with visual studio2015 under win10