Comparing StringBuffer content with equals
The equals
method of StringBuffer
is not overridden from Object
, so it is just reference equality, i.e., the same as using ==
. I suspect the reason for this is that StringBuffer
is modifiable, and overriding equals
is mostly useful for value-like classes that you might want to use as keys (though lists also have an overridden equals
and StringBuffer
is kind of a list, so this is a bit inconsistent).
How to compare two StringBuffer Objects using .equals() method?
Try
sb1.toString().equals(sb2.toString());
because StringBuffer#toString method returns the String value of the data stored inside the buffer:
Returns a string representing the data in this sequence. A new String object is allocated and initialized to contain the character sequence currently represented by this object. This String is then returned. Subsequent changes to this sequence do not affect the contents of the String.
How equals() method work in String Buffer?
Just look at the source code*
.
You will see that it just calls Object
's equals
public boolean equals(Object obj) {
return (this == obj);
}
Also consider using StringBuilder
see Difference between StringBuilder and StringBuffer
*
If using Eclipse ctrl-click on the Object and if the source coded is loaded in your system, it will take you there
I have 2 Same value object of String Buffer Class. String equals() method Showing False result Why?
There is no overriding of equals
in the StringBuffer
class. So it inherits the definition from Object
class. And from Java API we know its behavior:
The equals method for class Object implements the most discriminating
possible equivalence relation on objects; that is, for any non-null
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).
You have two different objects, so equals
return false
in this case.
String.equals(StringBuilder) and StringBuilder.equals(String) Confusion
No, String.equals
does not convert the other argument to a String
then compare the characters.
Compares this string to the specified object. The result is
true
if and only if the argument is notnull
and is aString
object that represents the same sequence of characters as this object.
If the other object is not a String
, e.g. a StringBuilder
, then it will always return false
. It will not convert the object to a String
.
Like any well-formed equals
method, it will test if the given object is the same class (ensuring it's not null
first), and if it is, cast it to a String
, but it won't call toString
or otherwise convert the given object to a String
.
Related Topics
How to Configure JPA for Testing in Maven
Jackson JSON Deserialization with Root Element
Access Restriction: Is Not Accessible Due to Restriction on Required Library ..\Jre\Lib\Rt.Jar
Explicit Type Casting Example in Java
What Is "Compiler Compliance Level" in Eclipse
How to Access Private Methods and Private Data Members via Reflection
How Much Data Can a List Can Hold at the Maximum
Using Jaxb to Cross Reference Xmlids from Two Xml Files
Copy All Values from Fields in One Class to Another Through Reflection
How to Avoid 'Instanceof' When Implementing Factory Design Pattern
Strange Java Null Behavior in Method Overloading
Java's Date(...) Constructor Is Deprecated; What Does That Mean
Loading Raw 64-Byte Long Ecdsa Public Key in Java
Byte Array and Int Conversion in Java
Double Calculation Producing Odd Result
Javamail API to Imail -- Java.Net.Socketexception: Permission Denied: Connect