• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

StringBuffer and equals()

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test110c {

public static void main(String [] args) {
StringBuffer sb1 = new StringBuffer("abcd");
StringBuffer sb2 = new StringBuffer("abcd");
System.out.println(sb1.equals(sb2));
}
}
Delivers "false".
Objects have same content. Does StringBuffer not override "equals" correctly?
Thomas
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I've got the answer:
posted February 24, 2001 10:00 AM
--------------------------------------------------------------------------------
No, StringBuffer.equals() does not behave in the same way as String.equals(). In the javadoc for StringBuffer, you can see that it just inherits the default implementation (Object.equals()), and The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
In other words, two stringBuffer1.equals(stringBuffer2) only if stringBuffer1 == stringBuffer2. If you need to compare, sort, convert or otherwise process a StringBuffer, turn it into a String first.
- Peter
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sb1.toString().equals(sb2.toString());
right?
---
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Robbie- right.
[Thomas]: Does StringBuffer not override "equals" correctly?
The override is correct. One of the requirements for equals() as defined in the Object API is that "It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified". If x and y refer to immutable objects, you can guarantee that two objects that are "equal" will always be equal - and so it makes sence to override equals() based on a comparison of the immutable attributes of the class. However if the class is not immutable, it's very difficult to guarantee that two "equal" objects will remain equal. In StringBuffer they correctly chose not to override the Object equals() method, because otherwise they couldn't guarantee a consistent equals() method.
 
Robbie shi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u,Jim Yingst
i got it~~
StringBuffer is not a consistant container~~
the length of the StringBuffer can change
after it was initialized..
i guess java compiler prevent you doing such
comparing
----
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
reply
    Bookmark Topic Watch Topic
  • New Topic