1. String Comparison in Java
String comparison means checking whether two Strings are equal or determining their alphabetical order.
Java provides multiple ways to compare Strings depending on the requirement.
2. Why String Comparison is Used
String comparison is important in many programming situations.
- a. Login validation
- b. Password checking
- c. Search systems
- d. Sorting text data
- e. Comparing user input
- f. Data filtering
3. Where String Comparison is Used
String comparison is used in almost every software application.
- a. Banking applications
- b. Web applications
- c. Mobile applications
- d. Database systems
- e. Online forms
- f. Search engines
4. Using == Operator
The == operator compares memory references, not actual String content.
4.1 Example Using String Literals
public class Main {
public static void main(String[] args) {
String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2);
}
}
The output is true because both variables point to the same String Pool object.
4.2 Example Using new Keyword
public class Main {
public static void main(String[] args) {
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1 == s2);
}
}
The output is false because two separate objects are created in memory.
The == operator should not be used for checking actual String values.
5. Using equals() Method
The equals() method compares actual String content.
5.1 Basic equals() Example
public class Main {
public static void main(String[] args) {
String a = "Java";
String b = new String("Java");
System.out.println(a.equals(b));
}
}
The output is true because both Strings contain the same characters.
5.2 Case-Sensitive Comparison
public class Main {
public static void main(String[] args) {
String s1 = "java";
String s2 = "Java";
System.out.println(s1.equals(s2));
}
}
The output is false because equals() is case-sensitive.
equals() checks character content, not object references.
6. Using equalsIgnoreCase() Method
The equalsIgnoreCase() method compares Strings without considering uppercase and lowercase differences.
6.1 Example of equalsIgnoreCase()
public class Main {
public static void main(String[] args) {
String s1 = "JAVA";
String s2 = "java";
System.out.println(s1.equalsIgnoreCase(s2));
}
}
The output is true because character case is ignored.
7. Using compareTo() Method
The compareTo() method compares Strings alphabetically.
7.1 Equal Strings
public class Main {
public static void main(String[] args) {
String a = "Apple";
String b = "Apple";
System.out.println(a.compareTo(b));
}
}
The output is 0 because both Strings are equal.
7.2 Smaller String
public class Main {
public static void main(String[] args) {
String a = "Apple";
String b = "Banana";
System.out.println(a.compareTo(b));
}
}
The output is a negative value because Apple comes before Banana alphabetically.
7.3 Greater String
public class Main {
public static void main(String[] args) {
String a = "Zoo";
String b = "Apple";
System.out.println(a.compareTo(b));
}
}
The output is a positive value because Zoo comes after Apple alphabetically.
8. Internal Working of String Comparison
Different comparison methods work differently internally.
- a. == compares object references
- b. equals() compares character sequences
- c. compareTo() compares Unicode values character by character
public class Main {
public static void main(String[] args) {
String s1 = "Cat";
String s2 = "Car";
System.out.println(s1.compareTo(s2));
}
}
The comparison happens character by character until a difference is found.
9. Using compareToIgnoreCase() Method
The compareToIgnoreCase() method compares Strings alphabetically while ignoring uppercase and lowercase differences.
9.1 Example of compareToIgnoreCase()
public class Main {
public static void main(String[] args) {
String s1 = "JAVA";
String s2 = "java";
System.out.println(s1.compareToIgnoreCase(s2));
}
}
The output is 0 because both Strings are considered equal after ignoring case differences.
10. Unicode-Based Internal Comparison
Java compares characters internally using Unicode values.
10.1 Understanding Unicode Comparison
| Character | Unicode Value |
| A | 65 |
| B | 66 |
| a | 97 |
| b | 98 |
Because uppercase and lowercase characters have different Unicode values, comparison results can differ.
10.2 Example of Unicode Difference
public class Main {
public static void main(String[] args) {
String a = "Apple";
String b = "apple";
System.out.println(a.compareTo(b));
}
}
The result is negative because uppercase A has a smaller Unicode value than lowercase a.
String comparison is based on Unicode values, not dictionary order.
11. Common Beginner Mistakes
11.1 Using == for Content Comparison
public class Main {
public static void main(String[] args) {
String a = new String("Java");
String b = new String("Java");
if(a == b) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
}
}
The output becomes Not Equal because == compares references.
11.2 Ignoring Case Sensitivity
public class Main {
public static void main(String[] args) {
String password1 = "Admin";
String password2 = "admin";
System.out.println(password1.equals(password2));
}
}
The output is false because equals() is case-sensitive.
11.3 Misunderstanding compareTo() Output
Many beginners think compareTo() returns only -1, 0, or 1.
Actually, it can return many positive or negative values depending on Unicode differences.
public class Main {
public static void main(String[] args) {
System.out.println("Cat".compareTo("Bat"));
}
}
The returned value depends on the Unicode difference between C and B.
12. Edge Cases in String Comparison
12.1 Comparing Empty Strings
public class Main {
public static void main(String[] args) {
String s1 = "";
String s2 = "";
System.out.println(s1.equals(s2));
}
}
The output is true because both Strings are empty.
12.2 Comparing with Null
public class Main {
public static void main(String[] args) {
String text = null;
System.out.println(text.equals("Java"));
}
}
This code throws NullPointerException because the variable contains null.
Always check for null before calling methods on String objects.
12.3 Safe Null Comparison
public class Main {
public static void main(String[] args) {
String text = null;
System.out.println("Java".equals(text));
}
}
The output is false and no exception occurs because the method is called on a valid String object.
13. Important Points to Remember
- a. == compares references
- b. equals() compares actual content
- c. equalsIgnoreCase() ignores case differences
- d. compareTo() performs Unicode-based comparison
- e. compareTo() can return large positive or negative values
- f. Null comparison can cause NullPointerException
- g. compareToIgnoreCase() ignores uppercase and lowercase differences
String comparison is an essential concept in Java because text validation, searching, filtering, and sorting are common tasks in real applications. Understanding the difference between reference comparison and content comparison helps developers avoid logical errors and write reliable Java programs.
