Would you please talk about your understanding of the Object class?

Object is the base class of all classes in java. It is the top of the entire class inheritance structure and the most abstract class. Everyone uses toString(), equals(), hashCode(), wait(), notify(), getClass() and other methods every day

1getClass() method

The first stage of class loading is to load classes Load the class file into memory and generate a Java Lang. class object. The getClass() method is to get this object.

2hashCode() method

This is a public method, so subclasses can override it. This method returns the hashCode value of the current object, which is a number in the integer range (- 2 ^ 31 ~ 2 ^ 31 - 1).

During the execution of a Java application, when the hashCode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used to compare the objects with equals has not been modified;

If the x.equals(y) method of two objects returns true, the hashcodes of X and Y objects must be equal.

If the x.equals(y) method of two objects returns false, the hashcodes of X and Y objects can be equal or unequal.

The default hashCode is the hash value that converts the memory address to. After rewriting, it is the user-defined calculation method;

3equals() method

public boolean equals(Object obj): used to compare whether the current object is equal to the target object. The default is to compare whether the reference points to the same object. It is a public method, and subclasses can be overridden.

public class Object{
    public boolean equals(Object obj) {
        return (this == obj);
    }
}

Why do I need to override the equals method?

Because if the equals method is not overridden, when a custom Object is placed in a map or set; If the hashcodes of the two objects are the same, the equals method will be called for comparison. At this time, the default equals method in the Object will be called. The default equals method only compares whether the references of the two objects point to the same Object. Obviously, they will not point to the same Object most of the time. In this way, duplicate objects will be stored in map or set. This destroys the feature that map and set cannot store duplicate objects, resulting in memory overflow.

Several conventions for overriding the equals method:

Reflexivity: that is, x.equals(x) returns true, and X is not null;
Symmetry: that is, the results of x.equals(y) and y.equals(x) are the same, and X and y are not null;
Transitivity: if the x.equals(y) result is true and the y.equals (z) result is true, then the x.equals(z) result must also be true;
Consistency: that is, x.equals(y) returns true or false. The results returned by multiple calls must be consistent without changing the parameters used by the equals method. X and y are not null.
If x is not null, x.equals(null) returns false.

4 clone() method

This method returns a copy of the current object.

This is a protected method provided to subclass overrides. However, it is necessary to implement the clonable interface, which is a tag interface. If it is not implemented, when calling object The clone () method will throw CloneNotSupportedException.

public class CloneTest implements Cloneable {
    private int age;
    private String name;
    //Omit get, set, constructor, etc
    @Override
    protected CloneTest clone() throws CloneNotSupportedException {
        return (CloneTest) super.clone();
    }
    public static void main(String[] args) throws CloneNotSupportedException {
        CloneTest cloneTest = new CloneTest(23, "9 Loong");
        CloneTest clone = cloneTest.clone();
        System.out.println(clone == cloneTest);
        System.out.println(cloneTest.getAge()==clone.getAge());
        System.out.println(cloneTest.getName()==clone.getName());
    }
}
//Output results
//false
//true
//true

From the output, we can see that the object of clone is a new object; However, the name of the String type of the original object and the clone object is the same reference, which indicates that super The clone method makes a shallow copy of the member variable if it is a reference type.

What is a shallow copy? Corresponding deep copy?

Shallow copy: copies are references.
Deep copy: new memory space is opened for value copy.

class Person implements Cloneable{
    private int age;
    private String name;
     //Omit get, set, constructor, etc
     @Override
    protected Person clone() throws CloneNotSupportedException {
        Person person = (Person) super.clone();
        //name opens up memory space through new
        person.name = new String(name);
        return person;
   }
}
public class CloneTest implements Cloneable {
    private int age;
    private String name;
    //Added person member variable
    private Person person;
    //Omit get, set, constructor, etc
    @Override
    protected CloneTest clone() throws CloneNotSupportedException {
        CloneTest clone = (CloneTest) super.clone();
        clone.person = person.clone();
        return clone;
    }
    public static void main(String[] args) throws CloneNotSupportedException {
       CloneTest cloneTest = new CloneTest(23, "9 Loong");
        Person person = new Person(22, "Monkey D Luffy");
        cloneTest.setPerson(person);
        CloneTest clone = cloneTest.clone();
        System.out.println(clone == cloneTest);
        System.out.println(cloneTest.getAge() == clone.getAge());
        System.out.println(cloneTest.getName() == clone.getName());
        Person clonePerson = clone.getPerson();
        System.out.println(person == clonePerson);
        System.out.println(person.getName() == clonePerson.getName());
    }
}
//Output results
//false
//true
//true
//false
//false

As you can see, even if the member variable is a reference type, we have implemented a deep copy. If the member variable is a reference type and wants to implement deep copy, the member variable should also implement the clonable interface and override the clone method.

5toString() method

Returns the string representation of the current object, which can be printed to facilitate viewing of object information, recording of log information, and debugging.

6finalize() method

This method is called by the JVM to clean up resources before garbage collection. This method may reset the object to reachable state, causing the JVM to fail garbage collection.

The finalize() method has the following four characteristics:

Never actively call the finalize() method of an object, which is called by the garbage collection mechanism itself;

It is uncertain when finalize() will be called and whether it will be called;

When the JVM executes finalize() of a recoverable object, it may change the object back to reachable state;

When an exception occurs when the JVM executes the finalize() method, the garbage collection mechanism will not report the exception, and the program continues to execute.

Keywords: Java Hibernate mvc

Added by mccark on Mon, 20 Dec 2021 05:18:04 +0200