当前位置:网站首页>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];
}
}
边栏推荐
- Record a simple question with ideas at the moment of brushing leetcode - Sword finger offer 09 Implementing queues with two stacks
- DPVS-FullNAT模式部署篇
- 聊聊swoole或者php cli 进程如何热重启
- Doris 运维中遇到的问题
- 利用swiper实现轮播图
- Tensorflow中CSV文件数据读取
- 用frp搭建云电脑
- Jenkins 发布PHP项目代码
- 第五章 习题(124、678、15、19、22)【微机原理】【习题】
- Px4 multi computer simulation (gazebo)
猜你喜欢

keil编译运行错误,缺少error:#5:#includecore_cm3.h_过路老熊_新浪博客

Summary of c++ references and pointers

Screen recording to GIF is an easy-to-use gadget, screentogif, which is free and easy to use!

OpenResty篇01-入门简介和安装配置
![搜索旋转数组II[抽象二分练习]](/img/db/3ea01cf1ad8446a7007891ef1d8e7f.png)
搜索旋转数组II[抽象二分练习]

UE4 learning record 2 adding skeleton, skin and motion animation to characters

流数据

mysql版本升级+数据迁移

SSM integrated learning notes (mainly ideas)

CXF
随机推荐
动态验证码
SSM整合学习笔记(主要是思路)
Two ways to center block level elements
Graduation trip | recommended 5-day trip to London
平衡二叉树AVL
php性能优化
Uniapp -- the use of document collation and push of unipush
数组常用的一些操作方法
谷歌浏览器(Chrome)最新v80版本下载
Audio basics and PCM to WAV
第六章 习题(678)【微机原理】【习题】
Apache Doris1.0版本集群搭建、负载均衡与参数调优
Leetcode-1528- rearrange string - hash table - string
Line height for small use
说说单例模式!
森林的先序和中序遍历
iomanip头文件在实战中的作用
Backup restore of xtrabackup
excel如何实现中文单词自动翻译成英文?这个公式教你了
SSM integrated learning notes (mainly ideas)