Java Review--object

Source: Internet
Author: User

Object has 9 methods to understand;

The following are the differences:

1) Public final native class<?> GetClass ();

2) public native int hashcode ();

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

4) protected native Object clone () throws Clonenotsupportedexception;

5) Public String toString () { return getclass (). GetName () + "@" + integer.tohexstring (Hashcode ());   }

6) Public final native void Notify ();

7) Public final native void Notifyall ();

8) protected void Finalize () throws Throwable {}

9) Public final native void wait (long timeout) throws interruptedexception;

There are three wait method overloads, but only one is really useful, and this is also an internal implementation native

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 >= 500000  | |   (nanos != 0 && timeout == 0)  {         timeout++;    }  &Nbsp; wait (timeout);     } 


The specific introduction of Object 9 methods;

1, public final native class<?> GetClass ();

Gets the class name of the object, namely: Class Package.class

2, public native int hashcode ();

Returns the memory address of the current object. Whenever you call Hashcode () on the same object, the same value should be generated, regardless of whether or not an internal change has occurred to the object.

3. PublicBoolean equals (Object obj) {return (this = = obj); }

Determines whether an object is equal by memory address.

4, protected native Object clone () throws Clonenotsupportedexception;

Object cloning, which is a method that must be implemented when deep cloning, and generally invokes the Clone method of object;

5. PublicString toString () { return GetClass (). GetName () + "@" + integer.tohexstring (Hashcode ());   }

return class name [16 binary number of email protected]+ address]


6, public final native void Notify ();

Thread Wakeup

7, public final native void Notifyall ();

Thread Wakeup

8, protected void Finalize () throws Throwable {}

Garbage collector to reclaim objects, the first to call this class's Finalize method, the general Java-written class does not need to re-overwrite this method, because object has implemented a default, unless we want to implement special functions.

9, public final native void wait (long timeout) throws interruptedexception;

Thread blocking, waiting for a certain amount of time

10. About Equals and hashcode

As long as you rewrite one of the two, you have another need to rewrite, but is not forced. The reason is that the Equals method uses the memory address comparison, and the same hashcode returns the memory address, if equal equals, but hashcode equality does not make sense, it should be equals true, then the value of hashcode must be the same , but not true. In addition, a big factor in the use of the map is the key, because we know the map key code:

    public v put (K key, v value)  {         if  (key == null)              return putfornullkey (value);        int  Hash = hash (Key.hashcode ());        int i =  Indexfor (hash, table.length);        for  (Entry<K,V>  e = table[i]; e != null; e = e.next)  {             Object k;                 V oldValue = e.value;                 e.value = value;                  e.recordaccess (This);                 return oldValue;             }         }        modCount++;         addentry (hash, key, value, i);         return  null;    }

The first comparison is hashcode, followed by equals, if you just rewrite the equals, not rewrite hashcode, should not get the expected consequences.


Java Review--object

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.