Saturday, 7 February 2015

Java Object calss and its methods

    Class object is the root of the class hierarchy. Every class has object as a superclass.  All objects ,including arrays, implement the methods of this class.
         class A //Implicitly inherits from Object class
                    {
                     }
 Following are the methods of the Object class, 

1) equals(Object)
 Syntax of equals method:

           public boolean equals(Object obj)
    
      Compares two Objects for equality.  This indicates whether some other object is "equal to " this one.
        Note that it is generally to override the hashCode() method whenever this method is overridden ,so as to maintain the general contract for the hashCode() method, which states that equal objects must have equal hash codes.


2) toString()

 Syntax of toString method:

           public String toString()
            
     Returns a string representation of the object.

     The toString() method for class Object returns a string consisting of the name of the class of which the object is an instance,the at-sign character '@' , and the unsigned hexadecimal representation of hash code of the object. In other words ,this method returns a string equal to the value of:
          getClass().getName()+'@'+Integer.toHexString(hashCode())



3) hashCode() 

 Syntax of hashCode method:

           public int hashCode()
     
        Returns a hash code value for the object.
       This method is supported for the benefit of hashtables such as those provided by java.util.HashTable.
       If two objects are equal according to the equals(Object) method,then calling the hashCode() method on each of the two objects must produce the same integer result.
       It is not required that if two objects are unequal according to the equals method,then calling the hashCode() method on each of the two objects must produce distinct integer results.


4) finalize()

 Syntax of hashCode method:

          protected void finalize () throws throwable

        Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
        Before an object is garbage collected, the runtime system calls its finalize() method. The intent is for finalize() to release system resources such as open files or open sockets before getting collected.
       Your class can provide for its finalization simply by defining and implementing a method in your class named finalize(). Your finalize() method must be declared as follows:


5) clone()

 Syntax of the clone() method:

          protected Object clone() throws CloneNotSupportedException
 
       Creates a new object of the same class as this object i.e copy of an object.
The java.lang.Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.    
      The clone() method saves the extra processing task for creating the exact copy of an object. If we perform it by using the new keyword, it will take a lot of processing to be performed that is why we use object cloning.


6) getClass()

       Returns the runtime class of an object.

7) wait()

      Waits to be notified by another thread of a change in this object.

8) notify()

     Wakes up a single thread that is waiting on this object's monitor.

9) notifyAll()

     Wakes up all threads that are waiting on this object's monitor.


Why wait,notify and notifyAll are in object class? 
         This one already explained in the previous post, please go through this link Why wait,notify and notifyAll are in object class?



Related Post:--