This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
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: 11253

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
Don't get me started about those stupid light bulbs.
subject: equals() method and == operator