The equals() method in Java is used to compare two strings for content equality. It checks whether two strings are exactly the same in terms of their characters and their order.
Syntax:
public boolean equals(Object obj)
Parameters: obj: The object to be compared with the current string.
Return Value:
- The method returns true if the string is equal to the specified object (i.e., both strings contain exactly the same sequence of characters).
- It returns false if the strings are not equal.
Example 1: Comparing Two Equal Strings
public class EqualsExample1 {
public static void main(String[] args) {
String str1 = "World";
String str2 = "World";
// Comparing two strings with equals()
if (str1.equals(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
}
}
Output: The strings are equal.
Explanation: In this example, str1.equals(str2) returns true because both strings have the same content (“World”).
Example 2: Comparing Strings are not same
public class EqualsExample2 {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";
// Comparing two different strings
if (str1.equals(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
}
}
Output: The strings are not equal.
Explanation: In this example, str1.equals(str2) returns false because “Hello” is not equal to “World”.
Example 3: Strings are Case Sensitive
public class EqualsExample3 {
public static void main(String[] args) {
String str1 = "world";
String str2 = "World";
// Comparing strings with different case
if (str1.equals(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
}
}
Output: The strings are not equal.
Explain the Example: Since equals() is case-sensitive, “world” is not equal to “World”, and the method returns false.
Example 4: Comparing String with == (Reference Comparison)
public class EqualsExample4 {
public static void main(String[] args) {
String str1 = new String("World");
String str2 = new String("World");
// Using equals() to compare contents
if (str1.equals(str2)) {
System.out.println("The strings are equal (equals method).");
} else {
System.out.println("The strings are not equal (equals method).");
}
// Using == to compare references
if (str1 == str2) {
System.out.println("The strings are equal (using ==).");
} else {
System.out.println("The strings are not equal (using ==).");
}
}
}
Explain the Example:
- str1.equals(str2) returns true because the contents of both strings are the same (“World”).
- str1 == str2 returns false because str1 and str2 are two different objects created using new String(), so they do not refer to the same memory location (reference inequality).
Example 5: Comparing String with null
public class EqualsExample {
public static void main(String[] args) {
String str1 = "World";
String str2 = null;
// Comparing a string with null
if (str1.equals(str2)) {
System.out.println("The strings are equal.");
} else {
System.out.println("The strings are not equal.");
}
}
}
Output: The strings are not equal.
Explanation: The equals() method will return false when one of the strings is null. If you call str1.equals(null), it will return false.