wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equal method 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equal method" Watch "Equal method" New topic
Author

Equal method

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442

Why the above code prints false?Does equal method print true only for String objects(ofcourse of same value)?
Thanks
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Veena,
StringBuffer uses the version of the equals method that is inherited from the Object class. It only compares references. It does not compare content.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
zarina mohammad
Ranch Hand

Joined: Jun 26, 2002
Posts: 104
Why the above code prints false?Does equal method print true only for String objects(ofcourse of same value)?

equals() method is overridden by the String class to perform content comparision.But StringBuffer does not override the equals() method to perform content comparision hence it prints false even the content being compared is same.
Gabriel Hanauer
Greenhorn

Joined: Jul 28, 2002
Posts: 1
For variables of object type, the "value" is taken as the reference to the object - typically, the memory address. you should not use this operators to compare the contents of objects, such as strings, because they will return true if two references refer to the same object, rather then if the two objects have an equivalent meaning.
From the RHE book
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
This strikes me as a misfeature. Any idea why the designers of StringBuffer didn't provide an override for equals() ?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Right, the equals() method in class StringBuffer is not implemented for god knows which reasons. There is a simple workaround, though, which is given below:
StringBuffer sb1 = new StringBuffer("www");
StringBuffer sb2 = new StringBuffer("www");
System.out.println(sb1.equals(sb2)); //prints false
System.out.println(sb1.toString().equals(sb2.toString())); //prints true


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
I think the reason why the equals() method of the StringBuffer class isn't overriden is because it's a mutable class, which means the values it contains can be changed after initialization, as opposed to, like, String and the primitive wrapper classes, which are immutable. If I'm not mistaken, it's in the difficulty in overriding the hashCode() method for mutable classes (making it reliable, threadsafe, etc.), which is required if you're gonna override the equals() method.
[ August 14, 2002: Message edited by: Paul Villangca ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Even more unfortunately, StringBuffer is a final class, so I can't just extend it and provide my own overriding equals() and hashCode().
I don't see the mutability as a real barrier to doing this right. StringBuffer.hashCode() could just return toString().hashCode() .
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by liborio:
For variables of object type, the "value" is taken as the reference to the object - typically, the memory address. you should not use this operators to compare the contents of objects, such as strings, because they will return true if two references refer to the same object, rather then if the two objects have an equivalent meaning.
From the RHE book

Then why does the following code prints true?

Thanks
Veena
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Because the String class overrides equals(). StringBuffer doesn't.
[ August 14, 2002: Message edited by: Ron Newman ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Hi Veena,
String (in contrast to StringBuffer) does implement equals. It does really check to see if the two strings are identical in content.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
StringBody doesn't.

Freudean slip?
Sorry Ron. Really.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Oops. I edited.
(StringBody was the name of a type in a long-dead language called Mesa that I used at Xerox 20+ years ago)
Talal Shaikh
Greenhorn

Joined: Jul 17, 2001
Posts: 13
So wat does the equals method in the object class really do? i thought it was comparing the state of two objects.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Object.equals() doesn't (and can't) know anything about the "state" of the two objects. All it can do is compare the two memory addresses.
Other classes can, and often should, override equals() to implement some useful notion of semantic equality. String does; StringBuffer (unfortunately) doesn't.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Are there any more classes like String classes which override equals() method for content comparison?
Thanks
Veena
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Yes. The wrapper classes -- Byte, Character, Integer, Double, and so on -- all override equals() to implement a sensible semantic comparison.
Paul Villangca
Ranch Hand

Joined: Jun 04, 2002
Posts: 133
Originally posted by Ron Newman:
...
I don't see the mutability as a real barrier to doing this right. StringBuffer.hashCode() could just return toString().hashCode() .

toString().hashCode() will return a different value if the value of StringBuffer is changed, so I don't think your solution is viable.
Cindy's segment in the Javaranch newsletter provides an, um, interesting explanation of overriding equals() and hashCode().
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Also it's interesting to know that the Wrapper and String classes implement java.lang.Comparable.
----------------
In support of Paul I think that using the hashCode with mutable classes implies one caution. Don't change the fields, on which hashCode and equals depend on, while the object is within a hash table structure. You migth find that the object is not retrievable from the container yet.


SCJP2. Please Indent your code using UBB Code
 
wood burning stoves
 
subject: Equal method