This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals

 
Ramya Iyer
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain this?

The Object class' equals() method just does an = = check, so if (ob1 = = ob2) is false, then ob1.equals(ob2) will always be false
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The == comparison operator tests values for equality. When applied to objects, that "value" is basically a reference (e.g., a memory address). Therefore, objA == objB if and only if they are, in fact, the same object (occupying the same space in memory).

The equals method is designed to be overridden to provide a more meaningful comparison of objects. For example, two separate instances of Car might be considered "equal" if they have specific variables in common (e.g., make, model, year, etc.). But in the simple case of Object, the equals method only checks whether the references point to the same object, just as the == operator does.
[ September 07, 2005: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic