Object class
- Superclass, base class, direct or indirect parent class of all classes, located at the top of the inheritance tree.
- If any class does not write extends to inherit a class, it will inherit 0bject class directly by default, otherwise it will inherit indirectly.
- The method defined in 0bject class is the method that all objects have.
- 0object type can store any object.
● as a parameter, any object can be accepted.
● as a return value, any object can be returned.
Common methods of Object
getClass() method
- public final Class<?> getClass() {}
- Returns the actual object type stored in the reference.
- Application: it is usually used to judge whether the actual storage object types in two references are consistent.
public class Test01 { private String name; private int age; public Test01(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String[] args) { Test01 test01 = new Test01("zheng",23); Test01 test02 = new Test01("li",22); Class class1 = test01.getClass(); Class class2 = test02.getClass(); if (class1 == class2){ System.out.println("Is the same type"); }else { System.out.println("Are different types"); } } }
result:
hashCode() method
- public int hashCode() {}
- Returns the hash code value of the object.
- Hash value is the number of int types calculated by hash algorithm according to the address or string or number of the object
Value. - Generally, the same object returns the same hash code.
System.out.println(test01.hashCode()); System.out.println(test02.hashCode());
toString() method
- public String toString() {}
- Returns the string representation (representation) of the object.
- This method can be overridden according to program requirements, such as displaying the attribute values of the object.
System.out.println(test01.toString()); System.out.println(test02.toString());
equals() method
- public boolean equals (0bject obj) {}
- The default implementation is (this == obj). Compare whether the addresses of two objects are the same.
- You can overwrite and compare whether the contents of the two objects are the same.
System.out.println(test01.equals(test02));
The equals() method overrides the steps
- Compare whether two references point to the same object.
- Judge whether obj is null.
- Judge whether the actual object types pointed to by the two references are consistent.
- Cast type.
- Compare whether the attribute values are the same in turn.
@Override public boolean equals(Object obj) { if (this == obj){ return true; } if (obj == null){ return false; } if (obj instanceof Test01){ Test01 test01 = (Test01)obj; if (this.name.equals(test01.getName())&&this.age == test01.getAge()){ return true; } } return false; }
finalize() method
- When the object is determined to be a garbage object, the JVM will automatically call this method to mark the garbage object and enter the collection queue.
- Garbage object: garbage object when there is no valid reference to this object.
- Garbage collection: GC destroys garbage objects to free up data storage space.
- Automatic recycling mechanism: the JVM runs out of memory and recycles all garbage objects at one time.
- Manual recycling mechanism: use system gc(); Notify the JVM to perform garbage collection.
new Test01("aaa",26); new Test01("bbb",26); new Test01("ccc",26); System.gc();