Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Wednesday, March 13, 2019

String equals() method in java with example - Internal Implementation

String equals() method in java with example:

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.


Java String equals()


Syntax:

public boolean equals​(Object anObject)


Overrides:

equals in Object class.

Parameters:

anObject - The object to compare this String against


Returns:

true if the given object represents a String equivalent to this string, false otherwise

See Also:  compareTo(String), equalsIgnoreCase(String)

Java String equals() method example:


package examples.java.w3schools.string;

public class StringEqualsExample {
 public static void main(String[] args) {
  String input1 = "hello";
  String input2 = "world";
  String input3 = "hello";
  
  // input 1 and 2 
  if (input1.equals(input2)) {
   System.out.println("Both input 1 and input 2 are matched");
  } else {
   System.out.println("input 1 and input 2 are not same");
  }

  // input 1 and 3 
  if (input1.equals(input3)) {
   System.out.println("Both input 1 and input 3 are matched");
  } else {
   System.out.println("input 1 and input 3 are not same");
  }
 }
}



Output:

input 1 and input 2 are not same
Both input 1 and input 3 are matched

equals() method internal code:

The following code is from String api. We will see now how it works internally in Java 8 and Java 11 versions.
1) Here first checks for the whether specified string address same as the current string. If same, returns true.
2) Checks if specified object (anObject) is instance of String, 
Java 8: if yes then compare lengths of both strings. if not same, returns false. If same then compares char by char in both strings. 
Java 11: if yes then checks coder of both strings are same. if same then checks the string is latin or UTF. If latin, it calls StringLatin1.equals() method. If UTF, it calss StringUTF16.equals() method.
3) Checks if specified object (anObject) is not instance of String, then returns false.

Java 8:



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;
    }



Java 11:




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;
    }



Please post your questions in comments section.

No comments:

Post a Comment

Please do not add any spam links in the comments section.