Reputation: 187
How does a StringBuffer handle strings internally? I tried running the below example and got the answer as "String are unequal".
From what I know is that the equals() returns true if both the strings have the same value. So what is happening in this case?
class StringBufferTest {
public static void main(String[] args) {
String newString = "HelloWorld";
StringBuffer buffer = new StringBuffer(newString);
if (buffer.equals(newString)) {
System.out.println("Strings are equal");
} else {
System.out.println("String are unequal");
}
}
}
Upvotes: 0
Views: 302
Reputation: 476
can not equals a String
Object with a StringBuffer
Object, use:
buffer.toString().equals(newString)
and better use StringBuilder
, because StringBuffer
not TheredSafe
Upvotes: 0
Reputation: 786091
You are comparing:
if (buffer.equals(newString))
Which is actually comparing a StringBuffer object type to a String object type. That will obviously return false.
In order to get true returned from equals
method you need to compare objects of same types.
public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null 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 objects is modified.
For any non-null reference value x, x.equals(null) should return false.
Upvotes: 0
Reputation: 85789
You're comparing an instance of StringBuffer
with an instance of String
, which won't give the desired results for being different types. Note that StringBuffer
will use the plain Object#equals
since it doesn't override it (noted by StringBuffer
JavaDoc against String#equals
that indeed overrides it). You must compare the String
content of the buffer
, not the object reference of your buffer
variable:
if (buffer.toString().equals(newString)) {
//...
}
Also, from Java 5, it would be better using StringBuilder
instead of StringBuffer
. More info about this: Difference between StringBuilder and StringBuffer
Upvotes: 8
Reputation: 83577
The equals()
contract implies that x.equals(y)
returns true if and only if x.getClass() == y.getClass()
. In other words, x
and y
must be instances of the same class. In this case, you are trying to compare a StringBuffer
to a String
. Comparing instances of two different classes will always be false
for a well-behaved equals()
method.
In order to do a meaningful comparison, you need to get the contents of the StringBuffer
:
buffer.toString().equals(newString)
Upvotes: 0
Reputation: 41281
equals(
returns false when two objects being compared are not the same type. No matter the contained string, StringBuffer
is a different type than String
and the comparison will never be true.
buffer.toString.equals(newString)
should work.
Upvotes: 2