wood burning stoves
The moose likes Beginning Java and the fly likes equals() method and == operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() method and == operator" Watch "equals() method and == operator" New topic

equals() method and == operator

Pradeep Balasubramanian
Ranch Hand

Joined: Jun 16, 2008
Posts: 75
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


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



Regards,<br />Pradeep Balasubramanian<br /> <br />You have to grow from the inside out. None can teach you, none can make you spiritual. There is no other teacher but your own soul. <br /> <br />- Swami Vivekananda
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link: http://aspose.com/file-tools
subject: equals() method and == operator
jQuery in Action, 3rd edition