当前位置:网站首页>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];
}
}
边栏推荐
- 4个要点,助力产品管好项目
- Stream in PHP socket communication_ Understanding of select method
- 权限设计=功能权限+数据权限
- Px4 simulation basis
- .user.ini文件导致的php网站安装问题
- STL教程5-STL基本概念及String和vector使用
- PHP interprocess pass file descriptor
- 实例:用C#.NET手把手教你做微信公众号开发(21)--使用微信支付线上收款:H5方式
- Audio basics and PCM to WAV
- 史上最简单的录屏转gif小工具LICEcap,要求不高可以试试
猜你喜欢
随机推荐
Today's 61 Fu
Px4 multi computer simulation (gazebo)
两种块级元素居中的方式
第六章 习题(678)【微机原理】【习题】
Backup restore of xtrabackup
The simplest screen recording to GIF gadget in history, licecap, can be tried if the requirements are not high
Static keyword explanation
Run the dronekit flight control application on Shumei faction under px4-jmavsim software simulation environment
Use Baidu map API to set an overlay (infowindow) in the map to customize the window content
idea 查看单元测试覆盖率
UE4 learning records create a role and control its movement
搜索旋转数组II[抽象二分练习]
IDEA常用快捷键
二叉排序树
Summary of c++ references and pointers
Px4 simulation basis
proxy
51 single chip microcomputer, some registers, some knowledge points
How does excel translate Chinese words into English automatically? This formula teaches you
7.常用指令(下)v-on,v-bind,v-model的常见操作









