Saturday 7 February 2015

Java Object calss and its methods

        Class object is the root of the class hierarchy. This is the super class of every java class.  All objects, including arrays, implement the methods of this class.

 public class A { //Implicitly inherits from Object class
 }

Following are the methods of the Object class, 

  • equals(Object)
 Syntax of equals method is

    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.

  • toString()
 Syntax of toString method is,

           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())

  • 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.

  • 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.



  • getClass()

       Returns the runtime class of an object.


  • wait()

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


  • notify()

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


  •  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:--

No comments:

Post a Comment