my dog learned polymorphism
The moose likes Beginning Java and the fly likes Difference in equals() method and comparision operator Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Difference in equals() method and comparision operator" Watch "Difference in equals() method and comparision operator" New topic
Author

Difference in equals() method and comparision operator

Vishnu Sharma
Ranch Hand

Joined: Feb 03, 2010
Posts: 55
I am not clear exactly about the equals() method of Object class and '==' operator and overlaoded equals() method and '==' operator


Regards,
Vishnu
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
The Object#equals(java.lang.Object) method looks something like this:Overloading the equals method is almost always a serious mistake.
You can override the equals method to take any sort of comparison you like. It is often necessary to override equals, but look in books like Bloch or google for Angelika Langer Java equals where you will find how careful you must be about it.
Brian Campbell
Greenhorn

Joined: Mar 26, 2010
Posts: 4
I'm not completely sure if this is the question you're asking, but it might be worth pointing out that when you compare two objects using the == operator, you are testing whether the two values refer to the same object. As Campbell pointed out, the "default" equals() method in the Object class does the same thing.

On the other hand, String and most other Java core classes define the equals() method so that it compares the actual values of objects. If you want equals() to do this for your user-defined classes, you have to override the equals() method in Object. As Campbell mentioned, getting that override correct might not be a trivial task.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32654
    
    4
Welcome to the Ranch, BC
Brian Campbell
Greenhorn

Joined: Mar 26, 2010
Posts: 4
Campbell Ritchie wrote:Welcome to the Ranch, BC


Thanks!
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Difference in equals() method and comparision operator
 
Similar Threads
Logic error
Differnce between == and equals
equals() method and == operator
Selecting a value in a combo box that equals the value from the database record?
equals() method and == operator