How does .equals Java String Class method work?

user10497264 :

In java there is a string class method that works as seen bellow. But I wonder how is it that we are able to call the method on a random variable of type string?

String word = "hello"
word.equals("hello");

output is true

Sandeepa :

The equals() method is used to verify if the state of the instances of two Java classes is the same. Because equals() is from the Object class, every Java class inherits it. But the equals() method has to be overridden to make it work properly. Of course, String overrides equals().

Take a look:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }

    if (anObject instanceof String) {
        String aString = (String)anObject;
        if (coder() == aString.coder()) {
          return isLatin1() ? StringLatin1.equals(value, aString.value)
            : StringUTF16.equals(value, aString.value);
        }
    }

    return false;
}

As you can see, the state of the String class value has to be equals() and not the object reference. It doesn’t matter if the object reference is different; the state of the String will be compared

I have taken it from here. Better if you have an idea about String pool also. So please refer the link

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Groovy == operator does not reach Java equals(o) method - how is it possible?

Java 8 distinct() does not invoke the equals method

How does the setOnCheckedChangeListener method work in JAVA?

How does dynamic method dispatching work in Java

Java equals() method - how does 'semantics of equals in subclasses' determine the use of getClass and instanceof

Java - equals method in base class and in subclasses

Does Java 7 switch statement with String use equals() method?

How to override equals method in Java

How does `Java` `List` method `size` work?

How does array class work in Java?

implement 2 flavors of Equals method for a class in Java

why does this java code not work if the method name is different from the class?

How does this JAVA 8 lambda method work?

How does map method work on string type?

Why equals method in the String class defined as equals(Object anObject) but not as equals(String anObject)?

How exactly does String.split() method in Java work when regex is provided?

How does "this" in work in Java when calling a method?

Java Equals Method doesnt work

Comparing String with equals() does not work

custom equals() method does not work properly

how equals method works in java

how does parseLong method work in java?

How does a synchronized method work in Java?

How does String.format method work in java?

How equals() method work in String Buffer?

How does this javascript class and method work?

How does the `Equals` work in typescript?

Calling method from instantiated class does not work, Java

How does newOutputStream method work in JAVA?