| Author |
equals() method and == operator
|
Ritika Pathak
Greenhorn
Joined: Jun 01, 2005
Posts: 8
|
|
Hi What is the difference between == operator and equals methods in case both are used with objects. i.e if == is applied to objects and equals() is also used with same objects then what will be the difference. Thanks Ritika
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Ritika Pathak: ... if == is applied to objects and equals() is also used with same objects then what will be the difference...
The == operator generates a compilation error if the references are not compatabile, whereas the equals method of Object will simply return false. Beyond that, if these objects are instances of classes that do not override the equals method, then the method should return the same result as the operator (a shallow comparison of references). But in most cases, classes should override equals to provide a meaningful comparison. [ June 01, 2005: Message edited by: marc weber ]
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Ritika Pathak
Greenhorn
Joined: Jun 01, 2005
Posts: 8
|
|
Hey Marc Thanks a lot
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 945
|
|
Originally posted by marc weber: But in most cases, classes should override equals to provide a meaningful comparison.
For example: The String.equals method compares the contents of the Strings, not the references. This should output "The Strings are equal()". Why have equals() in Object? So that data structure type objects have a method to call in any subclass of Object (i.e. ANY class) to check for containment. Internally Vector.contains() calls the equals() to determine if it does have the requested object. One last thing about equals(). If you override equals() in a class, take a close look at hashCode() too. If x.equals(y) returns true then, for the love of all that's holy, x.hashCode() had better return the same value as y.hashCode(). (Sounds like I got burned, doesn't it? I only wasted half a day tracking such an error down. I've heard tales of cases that were much worse.) There, is that too much information for you? Ryan [ June 01, 2005: Message edited by: Ryan McGuire ]
|
 |
ganesh pol
Ranch Hand
Joined: Apr 29, 2005
Posts: 151
|
|
hi ritika 1] == operater is applied to both primitive and non non primitive variables whereas equals() is method defined in object class and only used for non primitive variables 2]main diff betwn == and equals() when used for comparison == used for checking equality it results true if two reference point to same object while equals result in True if contents of object are same consider following String s1= "Ganesh"; String s2= "Ganesh"; String s3=new String("Ganesh"); here s1==s2 is true but s1==s3 is false and s1.equals(s3) is True
|
 |
Naren Chivukula
Ranch Hand
Joined: Feb 03, 2004
Posts: 542
|
|
Hi,
This should output "The Strings are equal()". Why have equals() in Object? So that data structure type objects have a method to call in any subclass of Object (i.e. ANY class) to check for containment.
The output which returns is The Strings are == The Strings are equal() !Because both s1 and s2 point to the same object and has same reference(This reference is stored in String Literal Pool. For more information of String Literal Pool go to http://www.javaranch.com/journal/200409/Journal200409.jsp#a1) Coming to "==" operator, it checks whether lvalue and rvalue are equal if they are primitives and it checks whether lvalue and rvalue have same references if they are objects Coming to .equals(), The method's default behaviour is same as "==" operator. i,e.. it checks for same refereces on objects!!! In reality this method is being overloaded to provide the functionality as it's name. So in String class it is being overrided such that if two strings have equal contents, then it returns true. Any comments are welcome. Regards, Narendranath Chivukula ----------------------------- Preparing for SCJP
|
Cheers,
Naren (SCJP, SCDJWS and SCWCD)
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 945
|
|
I stand corrected. My [s2="Rit" + "ika"] was obviously an attempt to avoid the String Literal Pool, but I guess Java is too smart for me. Ryan
|
 |
Mallesham Karnati
Ranch Hand
Joined: May 11, 2005
Posts: 40
|
|
QUOTE from GANESH POL: --------------------- here s1==s2 is true but s1==s3 is false and s1.equals(s3) is True --------------End Quote---- This is also true. Right? s1.equals(s2) is true. [ June 02, 2005: Message edited by: Mallesham Karnati ]
|
 |
Ryan McGuire
Ranch Hand
Joined: Feb 18, 2005
Posts: 945
|
|
Originally posted by Mallesham Karnati: QUOTE from GANESH POL: --------------------- here s1==s2 is true but s1==s3 is false and s1.equals(s3) is True --------------End Quote---- This is also true. Right? s1.equals(s2) is true.
Without even looking back at Ganesh's code that intialized the three Strings, we can be sure that if s1==s2 then definitely s1.equals(s2). This is true for Strings, which is what Ganesh's code used. It's POSSIBLE to declare equals() so that obj1==obj2 but obj1.equals(obj2) is false, but defining equals() in your class that way would constitute A Very Bad Thing Indeed. Ryan
|
 |
 |
|
|
subject: equals() method and == operator
|
|
|