File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringBuffer and equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringBuffer and equals()" Watch "StringBuffer and equals()" New topic
Author

StringBuffer and equals()

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
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

Joined: Mar 08, 2001
Posts: 192
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

Joined: Jan 05, 2003
Posts: 28
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

Joined: Jan 30, 2000
Posts: 18671
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.


"I'm not back." - Bill Harding, Twister
Robbie shi
Greenhorn

Joined: Jan 05, 2003
Posts: 28
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: StringBuffer and equals()