Java's parent class introduces: Source code Analysis of object class

Source: Internet
Author: User
Tags garbage collection instance method shallow copy
The object class is the parent class for all classes in Java, so it is important to know it.

Here's my understanding of the methods in the object class (combined with the comments in the code and some of the books I've seen)

public class Object {//Its main function is to map the methods in C/s + + to the native method in Java to realize the decoupling of the method name.    The execution of a function is performed in a static code block, which is executed when the class is first loaded.    private static native void Registernatives ();    static {registernatives ();    }//Returns the run-time object of this object, the returned Class object is locked by synchronized public final native class<?> GetClass (); /** * Features: * 1, whenever it is called on the same object multiple times during the execution of a Java application, the {@code Hashcode} method must always * return the same integer, provided that the letter used in the {@code equals} comparison on the object is not modified Interest.     From one * execution of an application to another execution of the same application, the integer does not need to be consistent.     * 2, if two object is determined by equals, then hashcode must return the same value * 3, if two object Equal,hashcode can be the same or different.    */Public native int hashcode ();     The Equals () method of/** * Object compares the address of an object in memory. * For all non-null references: * reflexive: X.equals (x) is true * symmetry: if X.equals (y) is true, then y.equals (x) is true * transitivity: if X.equals (y) is true,y. Equals (z) is true, X.equals (z) is true * Consistency: for any non-null reference value x and Y, assume that the information in the Equals comparison on the object has not been modified, call X.equals (y) multiple times Always returns TRUE or always returns false * for any non-null reference value x,x.equal (NULL) should return FALSE */public boolean equals (Object obj) {return (t His = = obj); }/** * Creates and returns a copy of an object, the correct invocation of clone () is the need to implement the Cloneable interface, and if the Cloneable interface is not implemented and the subclass directly calls the Clone () method of the object class, it throws Cloneno     Tsupportedexception exception.     * X.clone ()! = x * X.clone (). GetClass () = = X.getclass ();     * X.clone (). equals (x);     * These expressions are generally true, but they are not absolutely necessary. * In general, Clone () replicates this object once, becomes two objects, has its own memory space, instead of just establishing a reference, so X.clone ()! = x; True * X.clone (). GetClass () = X.getclas S (); True indicates that the run-time object of both objects is the same * x.clone.equals (x), and relies on the override of the Equals () method, which returns False if only the Equals () method of object is called.     Because the Equals () * Method of Object compares the address of the object. * Deep copy and shallow copy: * Shallow copy is to copy the data type field from the original object into the new object, copy the Reference field's reference to the new object, * Do not copy the "referenced object", so the original object and the new object refer to the same object, and the Reference field in the new object occurs Changes will cause the corresponding fields in the original object to change as well.     Deep copy is different in reference, deep copy is to create a new and original word * segment content of the same field, is two of the same size of the data segment, so the reference is different, and then the new object in the reference word paragraph changes, will not cause the original object field changes.    * */protected Native Object clone () throws Clonenotsupportedexception; Returns the name of the class plus @, followed by a 16-binary representation of the hash code of this class to public String toString () {return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ()); /** * Wakes a thread that is waiting on this object. If more than one thread is waiting for this object, select one of the threads to be awakened.     The selection is arbitrary and is determined by the JVM implementation.     * Thread waits for the object to be fetched by calling {@code wait} method. * The awakened thread (ready state) will not continue until the current thread abandons the lock on the object. The awakened thread will compete in a normal manner with any other thread that may be competing to synchronize this object *;     For example, a wake-up thread does not have a reliable privilege or disadvantage when it becomes the next thread that locks this object. * This method can only be called by the owner thread of the object, and the thread becomes the owner of the object in the following three ways: * 1, by executing the synchronous instance method of the object * 2, by executing the object's synchronous code block * 3, by executing the object's static synchronization method     。    * Only one thread can hold the lock on the object at the same time */public final native void Notify ();    Causes all threads waiting in the pool to wait for the same shared resource to exit from the wait state, enter the operational state, let them compete for the lock of the object, and only the thread that obtains the lock can enter the ready state public final native void Notifyall ();     /** * Causes the current thread to enter a wait state until the other thread calls the Notify () or Notifyall () method or reaches the specified time * The current thread must hold the lock on the object.     * This method causes the current thread t to enter the waiting queue for the object until the following four things occur: * 1, another thread called the Notify () method, and thread t was selected as the thread that woke up * 2, and other threads called the Notifyall () method * 3, other threads call the interrupt () method in the thread of thread T * 4, waiting longer than the time of the wait set.     However, if {@code timeout} is zero, real-time is not considered, and the thread waits only for other threads to wake. * After the thread is awakened, it is removed from the wait collection of object and can be re-dispatched * */public final native void wait (long timeout) throws interruptedexception; Public final void Wait (long timeout, int nanos) throws Interruptedexception {if (Timeout < 0) {thro        W New IllegalArgumentException ("Timeout value is negative");                                } if (Nanos < 0 | | Nanos > 999999) {throw new IllegalArgumentException (        "Nanosecond timeout value out of range"); } if (Nanos >= 500000 | | (Nanos! = 0 && Timeout = = 0))        {timeout++;    } wait (timeout);    }//Regardless of wait time, thread waits only for other threads to wake public final void Wait () throws interruptedexception {wait (0); }/** * This method is called when the garbage collector considers that an object has no other reference.     Subclasses override {@code Finalize} methods to process system resources or perform other cleanup. * In-depth understanding of Java Virtual machines: * Even objects unreachable in the accessibility analysis algorithm are not "dead". To actually declare an object to die, at least two times to go through the marking process: * 1. If the object finds no reference chain connected to the GC roots after the accessibility analysis, it will be first marked and filtered, and the filtered * condition is whether this object is required to perform finalize ( Method When the object does not overwrite the Finalize () method, or the Finalize () method has been called by the virtual machine *  The virtual machine considers the object to be dead. * 2, if the Finalize () method needs to be executed, then the object will be placed in a f-queue queue. The Finalize () method is the last opportunity for an object to escape death. The GC marks the object in the F-queue for a second time, and if the object succeeds in saving itself in the Finalize () method, the GC moves the object out of the "collections" collection when the second token is made; If the object does not escape, then it is really GG * Note that the Finalize () method only Can be called once by the system.     If the object faces a second garbage collection, the Finalize () method is not executed. */protected void Finalize () throws Throwable {}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.