Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Equal method

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why the above code prints false?Does equal method print true only for String objects(ofcourse of same value)?
Thanks
Veena
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This strikes me as a misfeature. Any idea why the designers of StringBuffer didn't provide an override for equals() ?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the String class overrides equals(). StringBuffer doesn't.
[ August 14, 2002: Message edited by: Ron Newman ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

StringBody doesn't.


Freudean slip?
Sorry Ron. Really.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 469
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any more classes like String classes which override equals() method for content comparison?
Thanks
Veena
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. The wrapper classes -- Byte, Character, Integer, Double, and so on -- all override equals() to implement a sensible semantic comparison.
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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().
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic