Signup/Sign In
PUBLISHED ON: MARCH 11, 2021

Java Program to Compare Strings

In this tutorial, we will learn how to compare two strings. But before moving further, if you are not familiar with the concept of string, then do check the article on Strings in Java.

Input: Enter the string: Apple

Enter the string: apple

Output: Both the entered string are equal: False

Program 1: Compare Two Strings

In this program, we will see how to compare two strings when the string is user-defined. This means firstly, we will ask the user to enter the string, and then by using the equals() method we will check whether the string entered is equal or not.

Algorithm

  1. Start
  2. Declare a string
  3. Ask the user to initialize it.
  4. Use the equals() method to compare two strings.
  5. Print the output.
  6. Stop

Below is the code for the same in Java language.

//Java Program to compare two strings
import java.util.*;
public class Main
{
    // Driver program 
    public static void main(String[] args) 
    {
        //Take input from the user
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the string: ");
        String s1=sc.nextLine();        
        System.out.println("Enter the string: ");
        String s2=sc.nextLine();
        //Compare two strings
        System.out.println("Are the two strings equal?" + s1.equals(s2));      
    }
}


Enter the string: hello
Enter the string: hello
Are the two strings equal? true

Program 2: Compare Two Strings

In this program, we will see how to compare two strings when the string is pre-defined. Here, we will use the == operator to compare the strings.

Algorithm

  1. Start
  2. Declare two strings
  3. Initialize them.
  4. Use the == operator to check whether the two strings are equal or not.
  5. Print the output.
  6. Stop.

Below is the code for the same in Java language.

//Java Program to compare two strings
public class Main
{
   public static void main(String []args)
   {
      String s1 = "Study tonight";
      System.out.println("The entered string is "+s1);
      String s2 = "Study tonight";
      System.out.println("The entered string is "+s2);
      System.out.println("Are the two strings equal?");
      //Compare the strings using == operator
      if(s1 == s2)
      System.out.println("Yes the entered strings are equal ");
      else
      System.out.println("No the entered strings are not equal ");
   }
}


The entered string is Study tonight
The entered string is Study tonight
Are the two strings equal?
Yes the entered strings are equal

Program 3: Compare Two Strings

In this program, we will see how to compare two strings when the string is pre-defined. Here, we will use different methods to compare the strings.

Algorithm

  1. Start
  2. Declare two strings
  3. Initialize the strings
  4. First, compare the strings using the equals() method.
  5. Print the result.
  6. Now, again compare by using the == operator.
  7. Here, compare the string and the value stored in another string.
  8. Display the output.
  9. Again, use the == operator to compare the strings.
  10. Compare both the strings.
  11. Display the output.
  12. Again, use the == operator to compare the strings.
  13. This time compare the values stored in both the string variables.
  14. Print the result.
  15. Stop.

Below is the code for the same in Java language.

//Java Program to compare two strings
public class Main
{
   public static void main(String []args)
   {   
        String str1 = new String("Study Tonight");
        System.out.println("The entered string is: "+str1);
        String str2 = new String("Study Tonight");
        System.out.println("The entered string is: "+str2);        
        System.out.println("Are the two strings equal? (using equals() method)");
        boolean result = str1.equals("Study Tonight"); // true
        System.out.println(result);
        System.out.println("Are the two strings equal? (Comparing by using == )");
        result = str2 == "Study Tonight"; // false
        System.out.println(result);
        System.out.println("Are the two strings equal? (Comparing by using == )");
        result = str1 == str2; // false
        System.out.println(result);
        System.out.println("Are the two strings equal? (Comparing Both Strings)");
        result = "Study Tonight" == "Study Tonight"; // true
        System.out.println(result);
    }
}


The entered string is: Study Tonight
The entered string is: Study Tonight
Are the two strings equal? (using equals() method)
true
Are the two strings equal? (Comparing by using == )
false
Are the two strings equal? (Comparing by using == )
false
Are the two strings equal? (Comparing Both Strings)
true

Program 4: Compare Two Strings

In this program, we will see how to compare two strings when the string is pre-defined. Here, we will use the compareTo() method to compare the strings. A point to be noted here is that, if the strings are equal, compareTo returns 0.

Algorithm

  1. Start
  2. Declare two strings
  3. Initialize them.
  4. Use the compareTo() method to check whether the two strings are equal or not.
  5. Print the output.
  6. Stop.

Below is the code for the same in Java language.

//Java Program to compare two strings
public class Main
{
   public static void main(String []args)
   {      
        String str1 = new String("Study Tonight");
        System.out.println("The entered string is: "+str1);
        String str2 = new String("Study Tonight");
        System.out.println("The entered string is: "+str2);
        System.out.println("Are the two strings equal?");
        System.out.println( str1.compareTo(str2) );
    }
}


The entered string is: Study Tonight
The entered string is: Study Tonight
Are the two strings equal?
0



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.