Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java String Comparison

In Java, strings are objects that represent sequences of characters. Comparing strings is a common task, and there are several ways to do it. In this tutorial, we will cover different methods to compare strings in Java.

  • Using equals() Method

The equals() method is part of the String class and is used to compare two strings for content equality. This method returns true if both strings contain the same sequence of characters, otherwise, it returns false.

Example:

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

        System.out.println(str1.equals(str2)); // Output: true
        System.out.println(str1.equals(str3)); // Output: false
    }
}
  • Using equalsIgnoreCase() Method

The equalsIgnoreCase() method is also part of the String class and is used to compare two strings for content equality, ignoring the case of the characters. This method returns true if both strings contain the same sequence of characters, ignoring their case.

Example:

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

        System.out.println(str1.equalsIgnoreCase(str2)); // Output: true
    }
}
  • Using compareTo() Method

The compareTo() method is part of the String class and is used to compare two strings lexicographically. This method returns a negative, zero, or positive integer depending on whether the invoking string is less than, equal to, or greater than the specified string. This method is useful for sorting strings.

Example:

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

        System.out.println(str1.compareTo(str2)); // Output: -1 (str1 is lexicographically less than str2)
        System.out.println(str1.compareTo(str3)); // Output: -32 (str1 is lexicographically less than str3 considering case)
    }
}
  • Using compareToIgnoreCase() Method

The compareToIgnoreCase() method is part of the String class and is used to compare two strings lexicographically, ignoring the case of the characters. This method returns a negative, zero, or positive integer depending on whether the invoking string is less than, equal to, or greater than the specified string, ignoring their case.

Example:

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

        System.out.println(str1.compareToIgnoreCase(str2)); // Output: -1 (str1 is lexicographically less than str2)
        System.out.println(str1.compareToIgnoreCase(str3)); // Output: 0 (str1 is lexicographically equal to str3 ignoring case)
    }
}

In summary, when comparing strings in Java, use the equals() method to check for content equality, the equalsIgnoreCase() method for case-insensitive content equality, and the compareTo() or compareToIgnoreCase() methods for lexicographic comparisons. Remember that the == operator should not be used for string comparison, as it checks for reference equality rather than content equality.

  1. Comparing strings in Java using equals method

    The equals method in Java is used to compare the content of two strings. It returns a boolean value indicating whether the strings are equal or not.

    String str1 = "Hello";
    String str2 = "World";
    
    boolean areEqual = str1.equals(str2);
    
    System.out.println("Are equal: " + areEqual);
    
  2. IgnoreCase in Java string comparison

    If you want to perform a case-insensitive string comparison, you can use the equalsIgnoreCase method.

    String str1 = "Hello";
    String str2 = "hello";
    
    boolean areEqualIgnoreCase = str1.equalsIgnoreCase(str2);
    
    System.out.println("Are equal (ignore case): " + areEqualIgnoreCase);
    
  3. Java compareTo method for string comparison

    The compareTo method is used for lexicographic comparison of strings. It returns an integer that indicates the difference between the two strings.

    String str1 = "apple";
    String str2 = "banana";
    
    int result = str1.compareTo(str2);
    
    System.out.println("Comparison result: " + result);
    
  4. Java string comparison using compareToIgnoreCase

    compareToIgnoreCase is similar to compareTo, but it performs a case-insensitive comparison.

    String str1 = "Apple";
    String str2 = "banana";
    
    int result = str1.compareToIgnoreCase(str2);
    
    System.out.println("Comparison result (ignore case): " + result);
    
  5. Comparing strings with null handling in Java

    When comparing strings with a possibility of null values, it's important to handle null cases to avoid NullPointerException. You can use the Objects.equals method for null-safe comparison.

    String str1 = "Hello";
    String str2 = null;
    
    boolean areEqual = Objects.equals(str1, str2);
    
    System.out.println("Are equal (with null handling): " + areEqual);
    
  6. Java String equals vs. contentEquals

    The equals method is used for comparing the content of two strings, while contentEquals is used to compare the content of a CharSequence (e.g., StringBuffer or StringBuilder) with a string.

    String str1 = "Hello";
    StringBuilder stringBuilder = new StringBuilder("Hello");
    
    boolean contentEqual = str1.contentEquals(stringBuilder);
    
    System.out.println("Content equal: " + contentEqual);