Object class and methods in Java Explained (java.lang.Object)

Object is the mother of all classes , in other words  every other class in java is the subclass of Object class.
Object class is in the default package i.e java.lang package .
 The Object class defines the basic state and behavior that all objects must have, such as the ability to compare oneself to another object, to convert to a string, to wait on a condition variable, to notify other objects that a condition variable has changed, and to return the object's class.
There are 11 methods in Object class .

It has following methods in its class



-----------------------------------------------------------------------------------
 Access                         ReturnType                         Methods                                           
Modifier
-----------------------------------------------------------------------------------


Public                             boolean                            *  equals(Object obj)

Public                             int                                     *  hashCode()

Protected                       void                                   *  finalize()

Public                            Class<?>                           *  getClass()

Protected                       Object                               *  clone()

Public                            void                                   *  notify()

Public                            void                                   *  notifyAll()

Public                            String                                 *  toString()

Public                           void                                     *  wait()

Public                           void                                     *  wait(long timeout)    

Public                           void                                     *  wait (long timeout , int nano )


----------------------------------------------------------------------------------


Now talk about its methods in general

1. equals (Object obj)

           Checks whether the obj object is equal to the object on which the equals method is called .

2.  hashCode()

            hashCode() is used  for the HashTable . It returns the hash value of the object.

3.  getClass()

           It returns the runtime class object .

4.  clone()

            It creates and  returns the copy of the object .    

5. notify()

            It will wake up the thread waiting for the objects monitor.

6. notifyAll()

            It will wakes up all the thread that are waiting for the objects monitor .

7.  toString()

           It will return the string representation of the object .

8. wait()

          This method causes the current thread to place itself in the wait set for this object and then to   
           relinquish any and all synchronization claims on this object




References :  Oracle Java Docs :

http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html


 

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry