MY mENU


Wednesday 30 January 2013

Equals() Method


Using “==” operator – it always compares objects with their referecnces.
Using “equals” method- it compares objects based on its implementation.

Why equals() method is specially given when == operator is already doing the objects comparison operation?
  • == operator will only compare reference of the objects.
  • Real world objects must be compared with their state.
  • For this purpose == operator should be overloaded.
  • Since java does not support operator overloading.
  • We need an alternative to == operator.
  • Hence equals() method is given by sun to provide.
  • object state comparison logic” by developer if need as per project requirement.
  • And every object may not contain state.

Object class developer does not know the sub class required object state comparison logic.

Hence it is the sub class developer’s responsibility to override equals() to compare subclass objects with their state.

If equals() method is overridden then hashCode() method must be overridden with below contract:

If equals() method return true by comparing two objects, then hashCode of the both objects must be same.

If it returns false, the hashCode() of the both objects may or may not be same.

Object Class


The object class is the root class of  the class hierarchy.

Why object class is the super class for all java classes?

Because of below 2 reasons object class is made as super class for every class.
Reusability: every object has 11 common properties. These properties must be implemented by every class developer. So to reduce burden on developer SUN developed a class called object by implementing all these 11 properties with 11 methods. All these 11 methods have generic logic common for all subclasses. If this logic is not satisfying subclass requirement then subclass can override it.

To achieve runtime polymorphism: so that we can write single method to receicve and send any type of class object as argument and as return type.

What are the common functionalities of every class object?

1. Comparing two objects:
Public Boolean equals(Object obj)

2. Retrieving hashcode(object identity)
Public native int hashCode()

3. Retrieving the runtime class object reference:
Public final native Class getClass()

4. Retrieving object information in String format for printing purpose:
Public String toString()

5. Cloning Object:
Protected native Object clone() throws CloneNotSupportException

6. Executing object clean-up code/ resource releasing code:
Protected void finalize() throws Throwalbe

7.8,9: Releasing object lock and sending thread to waiting state:
Public final void wait() throws InterruptedException
Public final native void wait(long millis) throws InterruptedException
Public final void wait(long millis,int nanos) throws InterruptedException

10,11:.Notify about object lock availability to waiting threads:
Public final native void notify()
Public final native void notifyAll()

All above operations are implemented with generic logic that is common for all objects, so that developer can avoid implementing these operation in every class, also SUN can ensure that all the operations are overridden by developers with the same method prototype. This feature will provide runtime polymorphism.

Developer should override these methods with object state.

We can override below five methods:
  1. Equals
  2. hashCode
  3. toString
  4. clone
  5. finalize


sun recommended to override equals,hashCode,toString method in all classes.