In this post, we are going to see how to compare two strings using Java. There are multiple ways we can compare strings in Java. I am going to show you the most commonly used ways here.
1. First method ( using equals() ) – Recommended:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * * @author agurchand */ public class CompareTwoStrings { /** * @param args the command line arguments */ public static void main(String[] args) { String str1 = "agur"; String str2 = "agur"; if ( str1.equals(str2) ) { System.out.println("Both are equal!"); } else { System.out.println("Both are not equal!"); } } } |
The output of the above program will be:
1 2 3 |
Both are equal! BUILD SUCCESSFUL in 302ms |
2. Second method ( using == ):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/** * * @author agurchand */ public class CompareTwoStrings { /** * @param args the command line arguments */ public static void main(String[] args) { String str1 = "agur"; String str2 = "babu"; if ( str1 == str2 ) { System.out.println("Both are equal!"); } else { System.out.println("Both are not equal!"); } } } |
The output of the above program will be:
1 2 3 |
Both are not equal! BUILD SUCCESSFUL in 302ms |
Let’s write another example Java program to get the strings from the user and then compare the strings.
We are going to use the Java Scanner to get the input from the user, so let’s see how we can do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Scanner; /** * * @author agurchand */ public class CompareTwoStrings { /** * @param args the command line arguments */ public static void main(String[] args) { String str1, str2; //create a scanner object to get the input Scanner in = new Scanner(System.in); //get the str1 value from user System.out.print("Enter the first string:"); str1 = in.next(); //get the str2 value from user System.out.print("Enter the second string:"); str2 = in.next(); //compare both using equals() if ( str1.equals(str2) ) { System.out.println("Both the strings are equal!"); } else { System.out.println("Both the strings are not equal!"); } } } |
Read the inline comments in the above Java Program to understand each and every line.
Here is another example using BufferReader:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * * @author agurchand */ public class CompareTwoStrings { /** * @param args the command line arguments * @throws java.io.IOException */ public static void main(String[] args) throws IOException { String str1, str2; //create a BufferReader object to get the input from user BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //get the str1 value from user System.out.print("Enter the first string:"); str1 = in.readLine(); //get the str2 value from user System.out.print("Enter the second string:"); str2 = in.readLine(); //compare both using equals() if ( str1.equals(str2) ) { System.out.println("Both the strings are equal!"); } else { System.out.println("Both the strings not equal!"); } } } |
Enjoy the day!