All functions of the Object method in Java (detailed version)

private static native void registerNatives();
    static {
        registerNatives();
    }

①registerNatives(): When a Java program needs to call a native method, the virtual machine locates and links the native method in the loaded dynamic file, so that the native method can be executed.

public final native Class<?> getClass();

②getClass(): Returns the runtime class of this Object. The returned Class object is the object locked by the static synchronized method representing the class.

public native int hashCode();

③hashCode(): returns the hash code value of the object.
The general convention for hashCode is:

  • Whenever the hashCode method is called more than once on the same object during the execution of a Java application, the method must consistently return the same integer, provided that information used in equals comparisons on the object has not been modified. This integer does not have to be consistent from one execution of the application to another execution of the same application.
  • If two objects are equal according to the equals(Obiect) method, then calling the hashCode method on both objects must produce the same integer result.
  • If two objects are not equal according to the equals(Object) method, then calling the hashCode method on the two objects must produce different integer results. However, programmers should be aware that producing different integer results for unequal objects may improve hash table performance.
public boolean equals(Object obj) {
        return (this == obj);
    }

④equals(): Indicates whether other objects are “equal” to this object. The equals method implements an equivalence relationship on a non-null object reference, and the return value is a Boolean type.

protected native Object clone() throws CloneNotSupportedException;

⑤clone(): Create and return a copy of this object. The exact meaning of “copy” may depend on the class of the object. The general intent is that for any object x, the expression: x.clone()!=x will be true and the expression: x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it’s often the case that: x.clone().equals(x) will be true, it’s not an absolute requirement.
By convention, the returned object should be obtained by calling super. It is a replica of it. This is the case for x.clon() if a class and all of its superclasses (except Object) follow this convention. GetClass() = xgetClass(). By convention, the object returned by this method should be independent of this object (being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Usually this means copying any mutable objects containing the internal “deep structure” of the object being cloned, and replacing references to those objects with references to the copies. If a class contains only primitive fields or references to immutable objects, it is usually the case that super.clone returns no fields in the object. Need to modify.

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

⑥toString(): Returns the string representation of the object. In general, the toString method returns a string that “textually represents” the object. The result should be a concise but informative representation that is easy for a human to read. It is recommended that all subclasses override this method.
The toString method of class object returns a string consisting of the name at-sign character d of the class of which this object is an instance and the unsigned hexadecimal representation of the object’s hash code. In other words, this method returns a string whose value is equal to: getClass().getName() + ‘@’ + Integer.toHexString(hashCode()) returns: the string representation of the object.

public final native void notify();

⑦notify(): Wake up a single thread that is waiting on this object monitor. If there are any threads waiting on this object, one is chosen to be woken up. The choice is arbitrary and occurs at the discretion of the exercise. A thread waits on an object’s monitor by calling one of its wait methods.
The awakened thread cannot proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual way with any other threads that might actively compete on this object for synchronization; for example, the awakened thread has no reliable privileges or disadvantages in being the next thread to lock the object.
This method can only be called by the thread that is the owner of this object’s monitor. A thread becomes the owner of an object monitor in three ways:

  • By executing a synchronized instance method of the object
  • By executing the synchronization statement body of the synchronization object
  • For objects of type class, only one thread at a time can own the object’s monitor by executing a synchronized static method of that class.
public final native void notifyAll();

⑧notifyAll(): Wake up all threads waiting on this object monitor. A thread waits on an object’s monitor by calling one of its wait methods.
The awakened thread cannot proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual way with any other threads that might be actively competing on this object for synchronization, e.g. the awakened thread does not have any solid privileges or disadvantages in being the next thread to lock the object. This method can only be called by the thread that is the owner of this object’s monitor.

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

⑨wait(long): Make the current thread wait until another thread calls the notify () method or notifyAll) method for this object, or the specified amount of time passes.
The current thread must own the monitor for this object. This method causes the current thread (call it 7) to place itself in the object’s wait set, and then relinquish any and all synchronization claims on the object. For thread scheduling purposes, thread T becomes disabled and lies dormant until one of the following four conditions occurs:

  • Some other thread calls the object’s notify method, which happens to be arbitrarily chosen as the thread to wake up.
  • Some other thread calls the notifyAll method for the object
  • Some other thread interrupts thread T.
  • The stated real time has passed, more or less. However, if the timeout is zero, the thread is just waiting for a notification regardless of real-time.

Thread Tis is then removed from the object’s wait set and thread scheduling is re-enabled. It then competes with other threads for synchronization rights to the object in the usual way, and once it has gained control of the object, all its synchronization claims on the object revert to their previous state – that is, to when the wait method was called status. Thread Tthen returns from calling the wait method. Therefore, when returning from the wait method, the synchronization state of the object and thread T is exactly the same as when the wait method was called. The thread can also be woken up without notification interruption or timeout, which is called False wakeup. Although this rarely happens in practice, the application must guard against it by testing for conditions that should cause the thread to be woken up, and continuing to wait if the conditions are not met.

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

⑩ wait (long, int): Make the current thread wait until another thread calls the notify (method or notifyAll () method for this object, or other threads interrupt the current thread, or a certain amount of actual time has passed. This method is similar to a parameter The wait method, but it allows finer control over the amount of time to wait for a notification before giving up. The real time amount, in nanoseconds, is given by 1000000*timeout + nanoseconds
In all other respects, this method does the same thing as the method wait(long) with one argument. In particular wait(0,0) and wait(0) mean the same thing. The current thread must own the monitor for this object. The thread releases ownership of this monitor and waits until one of the following two conditions occurs:

  • Another thread notifies threads waiting on the object’s monitor to wake up by calling the notify method or the notifyAll method.
  • The timeout period specified by the timeout milliseconds plus nanoseconds parameter has elapsed

The thread then waits until it can regain ownership of the monitor, and continues execution.

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

?wait(): Causes the current thread to wait until another thread calls the notify( method or the notifyAll() method for this object. In other words, this method behaves as if it just performed a call to wait(0). The current thread must Owns the object’s monitor. The thread releases ownership of this monitor and waits for another thread to notify a thread waiting on this object’s monitor to wake up by calling the notify method or the notifyAll method. The thread then waits until it can be reacquired Monitor ownership and continue execution.

protected void finalize() throws Throwable { }

?finalize(): Called by the garbage collector on an object when garbage collection determines that there are no longer references to the object. Subclasses override the finalize method to release system resources or perform other cleanup
The general contract of finalize is that when the Java”virtual machine has determined that any thread that has not died can no longer access the object in any way, except as a result of actions taken by the finalization of some other object or class that is preparing to finalize. The finalize method can do anything, including making the object available again to other threads; however the usual purpose of finalize is to perform cleanup operations before the object is irrevocably discarded. For example, the finalize method of an object representing an input/output connection might Perform an explicit l/0 transaction to disconnect before permanently discarding the object
The finalize method of the object class doesn’t do anything special; it just returns normally. Subclasses of object can override this definition.
The Java programming language makes no guarantees about which thread will call the finalize method of any given object. But it is guaranteed that when finalize is called, the thread calling finalize will not hold any user-visible synchronization locks. If the finalize method throws an uncaught exception, the exception will be ignored and the finalization of the object will terminate.
After calling the finalize method for an object, no further action is taken until the Java virtual machine has again determined that there is no way for any thread that has not yet died to access the object, including possible operations on other objects or classes that are ready complete, at which point the object may be discarded
For any given object, the Java virtual machine will not call the finalize method more than once, and any exception thrown by the finalize method will cause the termination of the object, but will be ignored in other cases.

Reference: java.lang.object.java

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Java skill treeHomepageOverview 108554 people are studying systematically