The String.equals()
method in Java is used to compare two strings for equality.
Table of Contents
- Introduction
equals
Method Syntax- Examples
- Basic Usage
- Case Sensitivity
- Using
equals
with Null Values - Real-World Use Case
- Conclusion
Introduction
The String.equals()
method is a member of the String
class in Java. It compares the specified string to the current string, checking if they have the same sequence of characters. This comparison is case-sensitive, meaning "Hello" and "hello" are considered different strings.
equals() Method Syntax
The syntax for the equals
method is as follows:
public boolean equals(Object anObject)
- anObject: The object to compare with.
Examples
Basic Usage
The equals
method can be used to compare two strings for equality.
Example
public class EqualsExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "World";
boolean result1 = str1.equals(str2);
boolean result2 = str1.equals(str3);
System.out.println("str1 equals str2: " + result1); // Output: true
System.out.println("str1 equals str3: " + result2); // Output: false
}
}
Output:
str1 equals str2: true
str1 equals str3: false
Case Sensitivity
The equals
method is case-sensitive, meaning it will only return true if the exact sequence, including case, is found.
Example
public class CaseSensitiveExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "hello";
boolean result = str1.equals(str2);
System.out.println("str1 equals str2 (case-sensitive): " + result); // Output: false
}
}
Output:
str1 equals str2 (case-sensitive): false
Using equals
with Null Values
When comparing strings, it is important to handle null values to avoid NullPointerException
.
Example
public class EqualsNullExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = null;
boolean result = str1.equals(str2);
System.out.println("str1 equals str2: " + result); // Output: false
}
}
Output:
str1 equals str2: false
Example: Safe Comparison
To safely compare a string with a potential null value, use a static string to call equals
.
public class SafeEqualsExample {
public static void main(String[] args) {
String str1 = null;
String str2 = "Hello";
boolean result = "Hello".equals(str1);
boolean result2 = "Hello".equals(str2);
System.out.println("Safe comparison with str1: " + result); // Output: false
System.out.println("Safe comparison with str2: " + result2); // Output: true
}
}
Output:
Safe comparison with str1: false
Safe comparison with str2: true
Real-World Use Case
Example: Validating User Input
One common use case for equals
is validating user input, such as checking if a username matches an expected value.
public class ValidateUserExample {
public static void main(String[] args) {
String expectedUsername = "admin";
String inputUsername = "Admin";
if (expectedUsername.equals(inputUsername)) {
System.out.println("Access granted.");
} else {
System.out.println("Access denied.");
}
}
}
Output:
Access denied.
In this example, the equals
method is used to check if the input username matches the expected username exactly.
Conclusion
The String.equals()
method in Java is a fundamental tool for comparing strings. It performs a case-sensitive comparison and returns true if the strings are identical in terms of character sequence and case. This method is particularly useful for validation and comparison tasks in various applications. By understanding and utilizing the equals
method, you can efficiently manage string comparisons in your Java programs.