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 {}