Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

StringBuffer and equals()

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Robbie shi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • 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
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic