How to compare strings in Java?

In Java, strings are compared using different methods depending on the type of comparison you want to perform. Here’s a detailed guide with examples:


1. Using equals() for Content Comparison

The equals() method compares the actual content of two strings for equality.

Example:

public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
String str3 = "hello";

// Case-sensitive content comparison
System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // false
}
}

Key Points:

  • equals() is case-sensitive.
  • Returns true if the content is the same; otherwise, false.

2. Using equalsIgnoreCase() for Case-Insensitive Comparison

The equalsIgnoreCase() method compares strings, ignoring case differences.

Example:

public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "hello";

// Case-insensitive content comparison
System.out.println(str1.equalsIgnoreCase(str2)); // true
}
}

Key Points:

  • Ignores case while comparing strings.
  • Useful for case-insensitive checks like user inputs.

3. Using compareTo() for Lexicographical Comparison

The compareTo() method compares strings lexicographically (dictionary order).

Example:

public class StringComparison {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = "Banana";
String str3 = "Apple";

// Lexicographical comparison
System.out.println(str1.compareTo(str2)); // Negative (-1 because "Apple" < "Banana")
System.out.println(str1.compareTo(str3)); // Zero (0 because "Apple" == "Apple")
System.out.println(str2.compareTo(str1)); // Positive (1 because "Banana" > "Apple")
}
}

Key Points:

  • Returns:
    • 0 if the strings are equal.
    • Negative value if the first string is lexicographically less than the second.
    • Positive value if the first string is lexicographically greater than the second.

4. Using compareToIgnoreCase() for Case-Insensitive Lexicographical Comparison

The compareToIgnoreCase() method compares strings lexicographically, ignoring case differences.

Example:

public class StringComparison {
public static void main(String[] args) {
String str1 = "Apple";
String str2 = "apple";

// Lexicographical comparison ignoring case
System.out.println(str1.compareToIgnoreCase(str2)); // 0 (ignores case)
}
}

5. Using == for Reference Comparison

The == operator checks if two string references point to the same memory location.

Example:

public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello"; // Points to the same string in the string pool
String str3 = new String("Hello"); // Creates a new string object

// Reference comparison
System.out.println(str1 == str2); // true (same reference)
System.out.println(str1 == str3); // false (different references)
}
}

Key Points:

  • == compares object references, not content.
  • Use equals() for content comparison instead of ==.

6. Checking if a String Contains Another String

Use the contains() method to check if one string contains another.

Example:

public class StringComparison {
public static void main(String[] args) {
String str1 = "Hello, world!";
String str2 = "world";

// Check if str1 contains str2
System.out.println(str1.contains(str2)); // true
}
}

7. Checking if a String Starts or Ends with a Substring

Use startsWith() or endsWith() for this purpose.

Example:

public class StringComparison {
public static void main(String[] args) {
String str = "Hello, world!";

// Check starting and ending
System.out.println(str.startsWith("Hello")); // true
System.out.println(str.endsWith("world!")); // true
}
}

8. Checking for Null or Empty Strings

Use isEmpty() or isBlank() (Java 11+).

Example:

public class StringComparison {
public static void main(String[] args) {
String str1 = "";
String str2 = " ";

// Check for empty or blank strings
System.out.println(str1.isEmpty()); // true
System.out.println(str2.isEmpty()); // false
System.out.println(str2.isBlank()); // true (Java 11+)
}
}

Best Practices for String Comparison

  1. Always use equals() or equalsIgnoreCase() for content comparison.
  2. Avoid using == for string comparison unless checking references.
  3. Use compareTo() or compareToIgnoreCase() for sorting or lexicographical comparisons.
  4. Handle null strings to avoid NullPointerException.

No images available.