top of page

How to compare strings in Java? 3 Methods

Are you curious about how to write a Java program to check if two strings are the same or not? String comparison is a common task in programming, and Java provides several methods to accomplish this. By understanding these methods and their usage, you can easily determine if two strings have identical content.


In this article "How to compare strings in Java?", we will explore various approaches to compare strings in Java and check for equality. We will cover different methods such as equals(), equalsIgnoreCase(), and the == operator, explaining how each method works and when to use them. By the end of this article, you will have a clear understanding of how to implement string comparison in Java and confidently determine if two strings are the same or not.


Let's explore string comparison in Java programming with 3 different methods!


How to compare strings in Java?

There are three methods using which you can compare the strings in Java:

  1. equals()

  2. equalsIgnoreCase()

  3. == operator


Method 1: equals() method

This method is defined in the Object class, which is the root class for all Java classes. It is used to compare the equality of two objects and determine whether they are considered equal based on their content.


For example:

import java.util.Scanner;

public class StringComparisonExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first string: ");
        String str1 = scanner.nextLine();

        System.out.print("Enter the second string: ");
        String str2 = scanner.nextLine();

        boolean areEqual = str1.equals(str2);
        System.out.println("Are the strings equal? " + areEqual);

        scanner.close();
    }
}

Output:

How to compare strings in Java? equals() method


Method 2: equalsIgnoreCase() method

The equalsIgnoreCase() method is a method defined in the String class. It is used to compare the equality of two strings while ignoring differences in case. It determines whether two strings have the same characters in a case-insensitive manner.


For Example:

import java.util.Scanner;

public class StringComparisonExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first string: ");
        String str1 = scanner.nextLine();

        System.out.print("Enter the second string: ");
        String str2 = scanner.nextLine();

        boolean areEqualIgnoreCase = str1.equalsIgnoreCase(str2);
        System.out.println("Are the strings equal (ignoring case)? " + areEqualIgnoreCase);

        scanner.close();
    }
}

Output:

How to compare strings in Java? equalsIgnoreCase() Method


Method 3: == operator

The == operator is a binary operator used for comparison. It is used to compare the equality of two operands, which can be primitive types or object references. The behavior of the == operator varies depending on the types of the operands being compared.


For Example:

import java.util.Scanner;

public class StringComparisonExample {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter the first string: ");
        String str1 = scanner.nextLine();

        System.out.print("Enter the second string: ");
        String str2 = scanner.nextLine();

        boolean areSame = (str1 == str2);
        System.out.println("Do the strings refer to the same object? " + areSame);

        scanner.close();
    }
}

Output:

How to compare strings in Java? == operator method

Conclusion

We can use three different methods in Java to check if two strings are the same or not: equals(), equalsIgnoreCase(), and the == operator. Each method has its own behavior and is suitable for different scenarios.


When deciding which method to use, consider the following guidelines:

  • If you want a case-sensitive comparison, use equals().

  • If you want a case-insensitive comparison, use equalsIgnoreCase().

  • If you want to check if the references of the strings are the same (i.e., they point to the same memory location), use the == operator.

Remember that the choice of method depends on the specific requirements of your program and the type of comparison you need to perform.

0 comments
bottom of page