Sunday 28 April 2013

Difference between equals() and ==

"equals()" and "==" are both used for checking the equality of two variables. 
A small difference is there though. 

  1. equals() checks for "value based equality" while "==" checks for reference equality.
  2. equals() is a method and can only be used in conjunction with an object while "==" is an operator and can be used for primitives and objects both
  3. equals() is a part of the Object class
  4. equals() can be overloaded and you can give your own meaning to it which is not possible with the "==" operator
  5. "==" when used with a primitive checks for the value help by the variables. On the other side when "==" is used with a reference variable it checks if the two reference variables point to the same object.

No comments:

Post a Comment