This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
for comparison between two objects we can use  operator 'obj1==obj2'  function 'obj1.equals(obj2)' but what is the difference between them? And if they represent the same funcationality what is the significance of equals() function?
Originally posted by saransh panda: for comparison between two objects we can use  operator 'obj1==obj2'  function 'obj1.equals(obj2)' but what is the difference between them? And if they represent the same funcationality what is the significance of equals() function?
the == operator actually tests the equality of the reference variables, that is, if the two variables are referring to the same object. To test the logical equality, that is, the equality of the object's state, we normally use the equals() method.
And welcome to the ranch [ May 12, 2008: Message edited by: Balasubramanian Chandrasekaran ]
Joined: May 02, 2008
thanks for the reply bryan but I think '==' operator can be used for comparing objects as well as primitives. In fact I have tried it and it works on objects as well.
Originally posted by Vishal Pandya: == checks wheather two object refernces points to same instance or not. Whereas equals() compare the actual contents of two string objects.
This is true for Strings but not for all objects. If a class does not override the equals method then it will inherit the equals method from the Object class. The equals method in the Object class compares the two objects using ==.