Stringbuffer.equals and String.equals difference in Java

Asked

Viewed 369 times

5

The behaviour of the method equals class StringBuffer Java is different from equals class String?

If yes, how would I overwrite that?

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

4

StringBuffer uses the implementation that comes from Object as the other answers demonstrate. But the comparison is not of a string and yes of a reference to a string. As a whole Object does so by default if the method is not overwritten. In other words, there is a comparison if the object is the same, if it is pointing to the same memory position. Of course, if you are in the same position, the content is the same, but the opposite may not be true. It is possible to have two equal contents in different positions of memory. So you test if two objects StringBuffer are equal in their content does not give a reliable result unless you write an algorithm for it (Math wrote in his answer). He may return false when in fact the content, in practice, is equal and only the reference is different.

Already String override the implementation by comparing each character individually (you can also see the code in the Math answer).

One string in Java if it’s the same you can have a system called interning. If it can be identified that there are two variables of the 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 for the reference and true for the content.

4

They are different.

Implementation of the method equals() 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 the method equals() in the Stringbuffer.java class:

//não há

So when you call the method equals() in a Stringbuffer object the method called is that of the Object class:

public boolean equals(Object obj) {
    return (this == obj);
}

The difference between them is that the equals() of the String class compares the contents of objects, i.e., the sequence of characters that the object stores, and returns true if they are equal, regardless of whether they belong to different objects. While the method equals() of the Stringbuffer class compares references, and only returns true if you’re comparing exactly the same object with itself, independent of its contents. (That question can better exemplify the behavior of equals() string class.)

You cannot overwrite either of the two, as the classes are marked as final which prevents them from being inherited and consequently having their methods superscripted. Each class has its specific purpose, if you want to compare strings just call the method equals() normally, however if you want to compare Stringbuffers, you will need to call the method toString() of each of them before calling the method equals(). 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()));
    }
}

Upshot:

false
true

-2

There is no difference because in both cases the method is the same. The method equals belongs to the class Object and both the class String and StringBuilder inherit the class Object.

To overwrite the method, the principle is the same to overwrite any method in the java language. Use notation @Override

  • 1

    The method equals() is overwritten in the String class, which makes it different from the method equals() object class.

  • 1

    Another thing I only remembered: tb is not possible to override the methods of the String and Stringbuffer classes because they are marked as final, so it’s not possible to inherit them. (I improved and deleted the previous comment because I needed to put a few words in the plural to make it clearer what I meant).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.