Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals() method and == operator

 
Pradeep Balasubramanian
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference in using the equals() method and == operator in java.

Why is there a need for equals() method when there is a == operator

or

Why is there a need for == operator when there is a equasl() method.

???

:roll:
 
fred rosenberger
lowercase baba
Bartender
Posts: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for objects, '==' compares if the references are equal - in other words, are they pointing to the same spot in memory. i.e. is it the exact same physical object.

the .equals() method, if it is properly defined, compares if two objects are effectively equal. Two $100 bills are both worth the same, even though they are distinct objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic