package java.lang; /** * Class {@code Object} is the root of the class hierarchy. * Every class has {@code Object} as a superclass. All objects, * including arrays, implement the methods of this class. * * @author unascribed * @see java.lang.Class * @since JDK1.0 */ 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 { } }
Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来。
在Java中,只有基本类型不是对象(数组也都扩展了Object类)。
/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类是不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone */ class Person{ private int age; public Person(int age){ this.age = age; } } public class test { public static void main(String... args) { Person p1 = new Person(20); Person p2 = new Person(20); System.out.println(p1 == p2); System.out.println(p1.equals(p2)); } }
输出:
false
false
equals比较的是两个对象是否指向同一个位置
重写equals方法:
/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone */ class Person{ private int age; public Person(int age){ this.age = age; } //根据Person类的年龄进行比较 @Override public boolean equals(Object obj){ if (!(obj instanceof Person)) { throw new ClassCastException("类型错误"); } Person p = (Person)obj; return (age == p.age); } } public class test { public static void main(String... args) { Person p1 = new Person(20); Person p2 = new Person(21); Person p3 = new Person(20); System.out.println(p1 == p2); System.out.println(p1.equals(p2)); System.out.println(p1.equals(p3)); } }
输出:
false
false
true
/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone */ class Person{ private int age; public Person(int age){ this.age = age; } //根据Person类的年龄进行比较 @Override public boolean equals(Object obj){ if (!(obj instanceof Person)) { throw new ClassCastException("类型错误"); } Person p = (Person)obj; return (age == p.age); } } public class test { public static void main(String... args) { Person p1 = new Person(20); Person p2 = new Person(21); Person p3 = new Person(20); Demo demo = new Demo(); System.out.println(p1 == p2); System.out.println(p1.equals(p2)); System.out.println(p1.equals(p3)); System.out.println("p1 = " + p1); System.out.println("p1.hashCode() = " + p1.hashCode()); System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode())); } }
输出:
false false true p1 = Person@28d93b30 p1.hashCode() = 685325104 Integer.toHexString(p1.hashCode()) = 28d93b30 |
一般重写equals方法后都需要重写HashCode,因为相等的两个对象必须确保hashCode相等。
/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone */ class Person{ private int age; public Person(int age){ this.age = age; } //根据Person类的年龄进行比较 @Override public boolean equals(Object obj){ if (!(obj instanceof Person)) { throw new ClassCastException("类型错误"); } Person p = (Person)obj; return (age == p.age); } @Override public int hashCode(){ return age; } } public class test { public static void main(String... args) { Person p1 = new Person(20); Person p2 = new Person(21); Person p3 = new Person(20); Demo demo = new Demo(); System.out.println(p1 == p2); System.out.println(p1.equals(p2)); System.out.println(p1.equals(p3)); System.out.println("p1 = " + p1); System.out.println("p1.hashCode() = " + p1.hashCode()); System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode())); } }
/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone */ class Person{ private int age; public Person(int age){ this.age = age; } //根据Person类的年龄进行比较 @Override public boolean equals(Object obj){ if (!(obj instanceof Person)) { throw new ClassCastException("类型错误"); } Person p = (Person)obj; return (age == p.age); } /* @Override public int hashCode(){ return age; } */ } public class test { public static void main(String... args) { Person p1 = new Person(20); System.out.println(p1); System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode())); } }重写toString方法:
/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone */ class Person{ private int age; public Person(int age){ this.age = age; } //根据Person类的年龄进行比较 @Override public boolean equals(Object obj){ if (!(obj instanceof Person)) { throw new ClassCastException("类型错误"); } Person p = (Person)obj; return (age == p.age); } /* @Override public int hashCode(){ return age; } */ public String toString(){ return "Person@@" + age; } } public class test { public static void main(String... args) { Person p1 = new Person(20); System.out.println(p1); System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode())); } }
输出:
Person@@20
Person@28d93b30