当前位置:网站首页>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];
}
}
边栏推荐
猜你喜欢
随机推荐
博图软件中多重背景块的建立_过路老熊_新浪博客
数据同步
Run the dronekit flight control application on Shumei faction under px4-jmavsim software simulation environment
期末复习【机器学习】
.user.ini文件导致的php网站安装问题
Static keyword explanation
为什么Integer的比较最好使用equals
今天说说String相关知识点
平衡二叉树AVL
QT Chinese and English use different fonts respectively
JS中的原型链面试题--Foo和 getName
proxy
流数据
Doris 运维中遇到的问题
Jenkins 发布PHP项目代码
idea Kotlin版本升级
Record the ideas and precautions for QT to output a small number of pictures to mp4
PHP interprocess pass file descriptor
Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!
iomanip头文件在实战中的作用









