当前位置: 首页 > news >正文

Java 根类 Object

java.lang.Object 是 Java 类层次结构中的根类,所有类都直接或间接实现了此类的方法。

Object API

源码

package java.lang;public class Object {private static native void registerNatives();static {registerNatives();}public final native Class<?> getClass();public native int hashCode();public boolean equals(Object obj) {return (this == obj);}protected native Object clone() throws CloneNotSupportedException;public String toString() {return getClass().getName() + "@" + Integer.toHexString(hashCode());}public final native void notify();public final native void notifyAll();public final native void wait(long timeout) throws InterruptedException;public final void wait(long timeout, int nanos) throws InterruptedException {if (timeout < 0) {throw new IllegalArgumentException("timeout value is negative");}if (nanos < 0 || nanos > 999999) {throw new IllegalArgumentException("nanosecond timeout value out of range");}if (nanos > 0) {timeout++;}wait(timeout);}public final void wait() throws InterruptedException {wait(0);}protected void finalize() throws Throwable { }
}

toString

返回一个对象的字符串表示。Object 中 toString 方法返回的是 类名 + “@” + 当前对象的哈希值,以此表示当前对象,建议所有子类重写该方法。

public String toString() {return this.getClass().getName() + "@" + Integer.toHexString(this.hashCode());
}

equals

判断其它对象是否等于当前对象,Object 中默认使用 == 判断,比较的是地址。

equals 方法在非空对象引用上实现相等关系:

  • 自反性:对于任何非空引用 x,x.equals(x) 都应返回 true
  • 对称性:对于任何非空引用 x 和 y,当且仅当 x.equals(y) 返回 true 时,y.equals(x) 也返回 true。
  • 传递性:对于任何非空引用 x、y 和 z,如果 x.equals(y) 返回 true,y.equals(z) 返回 true,那么 x.equals(z) 也返回 true
  • 一致性:对于任何非空引用 x 和 y,多次调用 x.equals(y) 都应返回 true,只要对象上 equals 信息没有修改
  • 对于任何非空引用 x,x.equals(null) 都应返回 false
public boolean equals(Object obj) {return this == var1;
}

String 类的 equals 重写:

public boolean equals(Object anObject) {if (this == anObject) {return true;}if (anObject instanceof String) {String anotherString = (String)anObject;int n = value.length;if (n == anotherString.value.length) {char v1[] = value;char v2[] = anotherString.value;int i = 0;while (n-- != 0) {if (v1[i] != v2[i])return false;i++;}return true;}}return false;
}

一般需要同时重写 equals 和 hashCode 方法,确保 equals 相等时 hashCode 相等。

如果 equals 判断两个对象相等,则 hashCode 相等;equals 不相等,hashCode 可能相等;hashCode 相等,equals 可能不相等。

hashCode

public native int hashCode();

String 类的 hashCode 重写:

private final char value[];private int hash; // Default to 0public int hashCode() {int h = hash;if (h == 0 && value.length > 0) {char val[] = value;for (int i = 0; i < value.length; i++) {h = 31 * h + val[i];}hash = h;}return h;
}

clone

clone 方法返回当前对象的副本对象。

protected native Object clone() throws CloneNotSupportedException;

Object 将 clone 作为一个本地方法来实现。当执行 clone 的时候,会检查调用对象的类(或者父类)是否实现了java.lang.Cloneable接口( Object 类不实现 Cloneable )。如果没有实现这个接口,将会抛出一个检查异常 — java.lang.CloneNotSupportedException,如果实现了这个接口,会创建一个新的对象,并将原来对象的内容复制到新对象,最后返回这个新对象的引用。

浅克隆与深克隆

Object 类的 clone 方法是浅克隆,对于字符串以外的引用数据类型克隆的是地址。

  • 浅克隆,对于引用类型,克隆的是地址
public class Human implements Cloneable {public String name;public int age;public Human mother;@Overridepublic Object clone() throws CloneNotSupportedException {return super.clone();}
}
public static void main(String[] args) throws CloneNotSupportedException {Human mother = new Human();mother.name = "gm";mother.age = 50;Human human = new Human();human.name = "kh";human.age = 25;human.mother = mother;Human copyMan = (Human) human.clone();System.out.println(human == copyMan); // falseSystem.out.println(human.name == copyMan.name); // trueSystem.out.println(human.mother == copyMan.mother); // true}
  • 深克隆,克隆对象是创建新的对象,然后复制对象内容。
public class Human implements Cloneable {public String name;public int age;public Human mother;@Overridepublic Object clone() throws CloneNotSupportedException {Human human = (Human) super.clone();if(mother != null) {human.mother = (Human) mother.clone();}return human;}
}
public static void main(String[] args) throws CloneNotSupportedException {Human mother = new Human();mother.name = "gm";mother.age = 50;Human human = new Human();human.name = "kh";human.age = 25;human.mother = mother;Human copyMan = (Human) human.clone();System.out.println(human == copyMan); // falseSystem.out.println(human.name == copyMan.name); // trueSystem.out.println(human.mother == copyMan.mother); // false
}

getClass

返回对象的运行时类。

public final native Class<?> getClass();

finalize

当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用。

protected void finalize() throws Throwable {}
http://www.lryc.cn/news/30562.html

相关文章:

  • 04_Apache Pulsar的可视化监控管理、Apache Pulsar的可视化监控部署
  • 【算法】期末复盘,酒店住宿问题——勿向思想僵化前进
  • Java中的Comparator 与 Comparable详解
  • 计算机科学导论笔记(二)
  • GEC6818开发板JPG图像显示,科大讯飞离线语音识别包Linux_aitalk_exp1227_1398d7c6运行demo程序,开发板实现录音
  • 如何判断树莓派通过GPIO与5G模块成功连接?
  • Java——包装类和List及ArrayList
  • matlab - 程序流程控制、函数文件、特殊函数、调试与优化
  • Toponogov 比较定理及其应用
  • 力扣sql简单篇练习(二十二)
  • 【开源硬件】STM32F030R8T6系统板
  • ES之DSL查询文档基础查询
  • 数据结构与算法之堆排序
  • Vue3 中的模板语法
  • Redis十大类型——Hash常见操作
  • Python采集本地二手房,一键知晓上万房源信息
  • Ubuntu 18.04 出现GLIBC_2.28 not found的解决方法(亲测有效)
  • Java文档搜索引擎总结
  • Linux内核学习笔记——页表的那些事。
  • C++,Qt分别读写xml文件
  • WebStorm安装教程【2023年最新版图解】一文教会你安装
  • 用户态和内核态,系统调用
  • Java 包装类
  • Raspberry Pi GPIO入门指南
  • 汇编语言程序设计(三)之汇编程序
  • 用二极管和电容过滤电源波动,实现简单的稳压 - 小水泵升压改装方案
  • 【数据结构与算法】数据结构有哪些?算法有哪些?
  • 使用Element-UI展示数据(动态查询)
  • lamda 表达式例子全集
  • 计算机网络第八版——第一章课后题答案(超详细)