Java String equals()
The Java String class equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.
The String equals() method overrides the equals() method of the Object class.
Signature
Parameter
anotherObject : another object, i.e., compared with this string.
Returns
true if characters of both strings are equal otherwise false.
Internal implementation
Java String equals() Method Example
FileName: EqualsExample.java
Output:
true false false
Java String equals() Method Example 2
The equals() method compares two strings and can be used in if-else control structure.
FileName: EqualsExample2.java
Output:
true both strings are unequal
Java String equals() Method Example 3
Let’s see one more example to test the equality of string present in the list.
FileName: EqualsExample3.java
Output:
Mukesh is present
Java String equals() Method Example 4
The internal implementation of the equals() method shows that one can pass the reference of any object in the parameter of the method. The following example shows the same.
FileName: EqualsExample4.java
Output:
false false false false true true true true