In this Java tutorial, you will learn how to check if two strings are equal using String.equals() and String.equalsIgnoreCase(), why == is not the usual choice for string value comparison, and how to handle null-safe string comparison with examples.
Java String Equality: Quick Answer
To check if two strings have the same text in Java, use the equals() method.
boolean result = str1.equals(str2);
If you want to compare strings without considering uppercase and lowercase differences, use equalsIgnoreCase().
boolean result = str1.equalsIgnoreCase(str2);
Do not use == when your goal is to compare the text stored in two strings. The == operator checks whether two references point to the same object.
Java – Check if two Strings are Equal
You can check if two strings are equal by considering case or by ignoring case in your Java application.
In this tutorial, we shall see how to check if two Strings are equal in Java using the method String.equals(String anotherString).
Also, we shall go through an example Java program to ignore the case of the characters in the string, and check if two Strings are equal.
Compare Two Strings in Java Using equals()
The equals() method compares the sequence of characters in two strings. It returns true when both strings contain the same characters in the same order, and false otherwise.
str1.equals(str2)
This comparison is case-sensitive. For example, "Java" and "java" are not equal when compared with equals().
Examples to Check if Two Strings Are Equal in Java
1. Check if two strings are equal
In the following example, we defined two strings, and then used String.equals() method. If two string are equal, which is an yes in the below example, equals() method returns true, else false.
Example.java
public class Example {
public static void main(String[] args) {
// string declaration
String str1 = "Hello World";
String str2 = "Hello World";
// str1.equals(str2) method returns true if str1 has same characters as that of str2
boolean areTwoStringsEqual = str1.equals(str2);
System.out.println("Are two strings equal : "+areTwoStringsEqual);
}
}
When the program is run, the output to the console would be as shown below.
Output
Are two strings equal : true
Here, both str1 and str2 contain the same text, so equals() returns true.
2. Check if two input strings are equal in Java
This example is same as that of previous one, but here we are reading strings entered by the user in console input.
Example.java
public class Example {
public static void main(String[] args) {
Scanner scanner = new Scanner(System. in);
//read first string
System.out.print("Enter first string : ");
String str1 = scanner.nextLine();
//read second string
System.out.print("Enter second string : ");
String str2 = scanner.nextLine();
//check if two strings are equal
boolean areTwoStringsEqual = str1.equals(str2);
System.out.print("Two strings are equal : "+areTwoStringsEqual);
}
}
If you are compiling this as a standalone Java file, add import java.util.Scanner; above the class declaration.
When the program is run, the output to the console is as shown in the following.
Output
Enter first string : good morning
Enter second string : good morning
Two strings are equal : true
The result is true because both entered strings are exactly the same, including lowercase letters and the space between the words.
3. Ignore case and check if strings are equal in Java
In this example, we shall ignore the case of the characters in two strings and check if strings are equal usgin equalsIgnoreCase() method of String class.
Example.java
public class Example {
public static void main(String[] args) {
// string declaration
String str1 = "Hello World";
String str2 = "hello world";
//ignore case and check if strings are equal
boolean areTwoStringsEqual = str1.equalsIgnoreCase(str2);
System.out.println("Two strings are equal : "+areTwoStringsEqual);
}
}
Run the program. As two strings are equal when you ignore the case, equalsIgnoreCase() should return true for the above example.
Output
Two strings are equal : true
Java String equals() vs == Operator
A common mistake is using == to compare strings. In Java, equals() checks the string content, while == checks whether both variables refer to the same object in memory.
Java Program
public class Example {
public static void main(String[] args) {
String str1 = new String("Java");
String str2 = new String("Java");
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
}
}
Output
false
true
Both strings contain the text Java, so equals() returns true. But the two variables refer to different string objects, so == returns false.
Check if Two Strings Are Not Equal in Java
To check if two strings are not equal, use the logical NOT operator ! before the equals() method call.
boolean notEqual = !str1.equals(str2);
Java Program
public class Example {
public static void main(String[] args) {
String str1 = "Java";
String str2 = "Python";
if (!str1.equals(str2)) {
System.out.println("Strings are not equal");
}
}
}
Output
Strings are not equal
Null-Safe String Equality Check in Java
If the first string may be null, calling str1.equals(str2) can throw a NullPointerException. One simple null-safe pattern is to call equals() on a known non-null string literal.
Java Program
public class Example {
public static void main(String[] args) {
String input = null;
boolean result = "admin".equals(input);
System.out.println(result);
}
}
Output
false
You can also use java.util.Objects.equals(str1, str2) when both variables may be null.
Java Program
import java.util.Objects;
public class Example {
public static void main(String[] args) {
String str1 = null;
String str2 = null;
boolean result = Objects.equals(str1, str2);
System.out.println(result);
}
}
Output
true
Case-Sensitive and Case-Insensitive String Equality in Java
Use equals() when uppercase and lowercase letters must match exactly. Use equalsIgnoreCase() when only the letters matter and case differences should be ignored.
| Requirement | Method to use | Example result |
|---|---|---|
| Compare exact text | equals() | "Java".equals("Java") returns true |
| Compare text with different case allowed | equalsIgnoreCase() | "Java".equalsIgnoreCase("java") returns true |
| Compare two possibly null strings | Objects.equals() | Objects.equals(null, null) returns true |
| Check whether two variables refer to the same object | == | Not usually used for string content comparison |
Common Mistakes When Comparing Strings in Java
1. Using == for string content comparison: Use equals() when you want to compare the actual text.
2. Forgetting that equals() is case-sensitive: "Hello" and "hello" are different for equals().
3. Calling equals() on a null reference: Use a string literal on the left side or use Objects.equals() when null is possible.
4. Confusing equality with ordering: equals() checks whether two strings are the same. If you need alphabetical ordering, use compareTo() or compareToIgnoreCase().
FAQs on Checking String Equality in Java
How do you check if two strings are equal in Java?
Use str1.equals(str2) to check whether two strings contain the same characters in the same order.
What does == do for strings in Java?
For strings, == checks whether two references point to the same object. It does not reliably check whether two strings contain the same text.
When should I use == and equals() in Java?
Use equals() for string content comparison. Use == only when you specifically want to check whether two references point to the same object.
Should I use equals() or equalsIgnoreCase() for string comparison?
Use equals() when case must match exactly. Use equalsIgnoreCase() when "Java" and "java" should be treated as equal.
How do you check if a Java string is not equal to another string?
Use !str1.equals(str2). If str1 may be null, use !Objects.equals(str1, str2) after importing java.util.Objects.
Editorial QA Checklist for Java String Equality Examples
- Confirm that every example using
equals()explains whether the comparison is case-sensitive. - Check that examples comparing strings with
==clearly explain reference comparison versus content comparison. - Verify that output blocks match the exact strings and boolean values printed by the Java program above them.
- Use
language-java syntaxonly for syntax-only snippets andoutputonly for program results. - Add a null-safety note when a string variable may be
nullbefore callingequals().
Conclusion
In this Java Tutorial, we learned how to check if two strings are equal in Java by considering the case of characters or ignoring it. Use equals() for normal string content comparison, equalsIgnoreCase() for case-insensitive comparison, and Objects.equals() when null values are possible.
TutorialKart.com