The structure of the Object

Class constructor

A class must have a constructor. The constructor is not visible in the source code of the Object class. The system automatically adds a constructor with no parameters.

Object obj = newThe Object ();Copy the code

equals()

If we want to compare the contents of two objects, we will override this method. If we want to compare the contents of two objects, we will override this method. * Note: When overriding equals(), you must also override hashCode (). Since Java dictates that two objects are equal, their Hashcodes must also be equal. * /   
	public boolean equals(Object obj) {
        return (this == obj);
    }
Copy the code

getClass()

/** * This method is final and cannot be overridden by subclasses. It returns the class in which the object was run
public final nativeClass<? > getClass();Copy the code

hashCode()

/** * is used to determine whether two objects are equal * 1: if two objects are equal, their hashcodes must be equal * 2: If hashcodes are equal, the two objects must be equal * 3: if hashcodes are not equal, the two objects must be equal */
public native int hashCode(a);

Copy the code

toString()

public String toString(a) {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
Copy the code

GetClass ().getName() is the full class name (including the package name) of the returned object,

Integer.tohexstring (hashCode()) returns a string representation of this hashCode as a hexadecimal unsigned Integer.

When printing an object, the default is to call the toString method.

notify()

/** * final modifier, which cannot be overridden, wakes up a thread that is waiting for an object lock. This method can only be used for synchronous methods. * /
public final native void notify(a);

Copy the code

notifyAll()

/** * final modifier, which cannot be overridden, wakes up all threads waiting for an object lock. This method can only be used for synchronous methods. * /
public final native void notifyAll(a);

Copy the code

wait()

/** * Causes the current thread to wait before another thread calls the notify() or notifyAll() method of this object. * The current thread must own this object monitor. * The thread publishes ownership of the monitor and waits until another thread tells the waiting thread on the object's monitor to wake up by calling notify or notifyAll. * Then the thread will wait until it regains ownership of the monitor before continuing. * /
public final void wait(a) throws InterruptedException {
    wait(0);
}

Copy the code

finalize()

/** * This is supported by the JVM when objects are reclaimed. The Object Finalize method defaults to doing nothing. * Subclasses can override the Finalize method if they need to perform some logical processing when objects are reclaimed. * /
protected void finalize(a) throws Throwable {}Copy the code

registerNatives()

    private static native void registerNatives(a);
    static {
        registerNatives();
    }
Copy the code

This is a local method, and we know that after a class defines a local method, it must also load the local library in order to call the operating system implementation.

If you look at the code above, it makes sense. Static code blocks are what a class is bound to execute during initialization, so they are executed when the class loads, and local methods are registered through this method.

Refer to the link

www.cnblogs.com/ysocean/p/8…

www.zhihu.com/question/21…