Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP 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