[Java] [commonly used class] Object base class source code learning

Source code overview:

Many are native methods, written behind C ++

 

There is no description about the constructor, the parameterless construction provided by the default compiler

https://blog.csdn.net/dmw412724/article/details/81477546

Ah, why is it still native? The only conclusion that can be drawn at present is that all the methods of native modification are implemented by JNI calling C ++ or C code

 

Get class objects for reflection and reading configuration files

public final native Class<?> getClass();

 

Get the hash value of the object, similar to the address value of C, because it is running in the JVM, so it is not the real memory address value, it is virtual

 -Can be used to help determine whether two objects are the same

public native int hashCode();

 

Original equal judgment, directly take the address to compare

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

 

Clone method, return a copied object, in addition to the second method of obtaining objects in the new way

-Note that cloning exceptions are not supported, this method needs to implement the Cloneable interface before it can be used

protected native Object clone() throws CloneNotSupportedException;

 

The default toString method

-The full class name of the class to which the object belongs and the hash value converted into hexadecimal are returned

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

 

Used for multi-threaded development, method meaning notification

[Notify a thread waiting on the object to return from the wait method, and return the lock that needs to be acquired for the object]

    public final native void notify();

 [Notify all threads waiting on the object]

    public final native void notifyAll();

[Make the object wait within the time of the parameter value, return overtime, or be notified within the waiting time, pay attention! The wait method will release the lock of the object]

 public final native void wait(long timeout) throws InterruptedException;

[Further accuracy of timeout]

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);
    }

[Enter the wait state until it is notified by other threads or interrupted before returning]

public final void wait() throws InterruptedException {
        wait(0);
    }

 

[Method executed when the object is destroyed]

protected void finalize() throws Throwable { }

 

Guess you like

Origin www.cnblogs.com/mindzone/p/12715460.html