This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals() method and == operator" Watch "equals() method and == operator" New topic
Author

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

or

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

???

:roll:


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
Bartender

Joined: Oct 02, 2003
Posts: 11169
    
  16

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
 
 
subject: equals() method and == operator
 
Similar Threads
problem with if-condition
Question on String Buffer
toString()
equals() method and == operator
Constant - Char & String