Object Class,
1 Overview of Object classes
- Class Object is the root class of the class hierarchy. Each class uses
Object
As a superclass. All objects (including arrays) implement this class method.
- Object classes are constantly extracted.
2 equals () method
public boolean equals(Object obj)
public boolean equals(Object obj) { return (this == obj); }
- Example: Call the equals () method of the Object class
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Person p2 = new Person ("Zhang San", 15); System. out. println (p = p2); // false System. out. println (p. equals (p2); // false }}
- Example: override the equals () method
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Person p2 = new Person ("Zhang San", 15); System. out. println (p = p2); // false System. out. println (p. equals (p2); // true }}
3 hashcode () method
public int hashCode()
public native int hashCode();
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Person p2 = new Person ("Zhang San", 15); System. out. println (p); // java011.Person @ 1b6d3586 System. out. println (p. hashCode (); // 460141958 System. out. println (Integer. toHexString (p. hashCode (); // 1b6d3586 }}
4 getClass () method
public final Class<?> getClass()
public final native Class<?> getClass();
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); Class <?> Clazz = p. getClass (); System. out. println (clazz); // class java011.Person System. out. println (clazz. getName (); // java011.Person }}
5 toString () method
public String toString()
public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); }
Package java011;/**** * Description: */public class Person {private String name; private int age; public Person () {} public Person (String name, int age) {this. name = name; this. age = age ;}@ Override public boolean equals (Object obj) {if (obj = null) {return false;} if (obj instanceof Person) {Person p = (Person) obj; if (! This. name. equals (p. name) & this. age! = P. age) {return false ;}} return true ;}}
Package java011;/***** * Description: */public class Test {public static void main (String [] args) {Person p = new Person ("James ", 15); System. out. println (p); // java011.Person @ 1b6d3586 System. out. println (p. toString (); // java011.Person @ 1b6d3586 }}