Java Object Source Parsing

Source: Internet
Author: User

Object class as the root of all class hierarchy, has a very important role, each class let object as its superclass, all objects including arrays, all implement the method of object definition, in a word, where there are objects must realize the object class method

First we know that there are several methods in the object class:

Class<?> getclass (); Returns the running class of the current object

int hashcode (); Returns the hash value of the object

Boolea equals (Object obj); Compares whether other objects are equal to this object

Protected Object clone (); Creates and returns a copy of this object

StrinG toString (); Returns the string representation of the object

void Notify (); Wakes up a single thread waiting on this object listener

void Notifyall (); Wake up all the threads waiting on this object listener

void wait (long timeout); When another thread calls this object's notify () method or the Notifyall () method, or exceeds the specified time, the current thread waits

void Wait (long timeout, int nanos); The current thread waits when another thread calls the Notify () method of this object or the Notifyall () method, or exceeds the time limit, or when the thread is interrupted by one of the other threads

void Wait (); Causes the current thread to wait when another thread calls this object's notify () method or the Notifyall () method

protected void Finalize (); This method is called by the object's garbage collector when the garbage collector determines that no more references to the object exist


Java.lang.Object Source

Package Java.lang;public class Object {    private static native void Registernatives ();//a local method, The implementation is called when the C or C + + implementation     //object is initialized and is called only once     static {         Registernatives ();    }    public final native class<?> getclass ();   & nbsp;//returns different integers for different objects (typically implemented by converting the object's internal address to an integer)     public native int hashcode ();     public boolean equals (Object obj) {        return (this = = obj);    }     protected native Object clone () throws clonenotsupportedexception;    public String ToString () {        return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ());     }    public Final native void notify ();    public final native void Noti Fyall ();    public final native void wait (long timeout)Throws interruptedexception;    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) {       &nbsp ;    throw New IllegalArgumentException (                      "nanosecond timeout value out of range");        }& nbsp;       if (nanos >= 500000 | | (Nanos! = 0 && Timeout = = 0)) {            timeout++;        }         wait (timeout);    }    public Final void Wait () throws Interruptedexception {        wait (0);    }     protected void Finalize () throws Throwable {    }
We see that many of the methods inside the object class are decorated with native, indicating that it is a local method, which is implemented by non-Java code, and it is worth mentioning that the Equals () method and the Hashcode () method in the object class, when we compare whether two objects are equal, Both methods are used to compare whether two objects are equal, one is to compare the reference of two objects with the Equals method, that is, to compare the value of the object address stored in two references is equal, and then use Hashcode () The Hashcode method compares the values of their stored addresses (which are actually compared by the hash value of an int type returned by the.), if the reference is equal and the address is equal, then the two objects are equal

Some classes in the JDK override the Equals method so that it can directly compare two objects for equality, such as some classes in the collection


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Object Source Parsing

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.