The behavior of method equals
of class StringBuffer
of Java is different from equals
of class String
?
If yes, how would I overwrite it?
The behavior of method equals
of class StringBuffer
of Java is different from equals
of class String
?
If yes, how would I overwrite it?
They are different.
Implementation of the equals()
method in the String.java class:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String) anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
Implementation of method equals()
in class StringBuffer.java:
//não há
So when calling the equals()
method on an object of type StringBuffer the method called is the one of the class Object:
public boolean equals(Object obj) {
return (this == obj);
}
The difference between them is that the equals()
of the String class compares the contents of the objects, that is, the sequence of characters that the object stores, and returns true if they are equal, independent if they belong to different objects. While the equals()
method of the StringBuffer class compares the references, and only returns true if you are comparing exactly the same object with itself, regardless of its contents. ( This question can best illustrate the behavior of equals()
of the String class.)
You can not override any of the two, because classes are marked as final
, which prevents them from being inherited and consequently having their methods overwritten. Each class has its specific purpose, if you want to compare Strings, you just have to call the equals()
method normally, however if you want to compare StringBuffers, you need to call the toString()
method of each of them before calling the equals()
method. Example:
public class CompStrings {
public static void main(String[] args) {
StringBuffer s1 = new StringBuffer("teste");
StringBuffer s2 = new StringBuffer("teste");
System.out.println(s1.equals(s2));
System.out.println(s1.toString().equals(s2.toString()));
}
}
Result:
false true
StringBuffer
uses the implementation that comes from Object
as the other responses demonstrate. But the comparison is not from a string but from a reference to a string . As all Object
does by default if the method is not overwritten. That is, there is a comparison if the object is the same if it is pointing to the same memory location. Of course, if you are in the same position, the content is the same, but the reverse may not be true. It is possible to have two equal contents in different positions of the memory. So you test whether two StringBuffer
objects are equal in their content does not give a reliable result unless you write an algorithm for it (The Math wrote in its response). It can return false
when in fact the content in practice is equal and only the reference is different.
Already String
overwrite the implementation with its own comparing each character individually (you can also see the code in the Math response).
A string in Java if same may have a system called interning . If it can be identified that there are two variables of type string
equal, there will not be two allocations, there will be only one object and the variables will point to the same memory location. In this case both the reference and the content will return true
. In other cases, you can return false
to the reference and true
to the content.
There is no difference because in both cases the method is the same. The equals
method belongs to the class Object
and both the class String
and StringBuilder
inherit class Object
.
To override the method, the principle is the same to override any method in the java language. Use the notation @Override