String Comparison in Java

You are currently viewing String Comparison in Java

Hello reader! Are you going to compare the two strings using the java program? If yes refer to this article. Here, I’m going to explain a string comparison and how to do string comparison in java program?

Basically, The strings contain letters, numbers, and spaces. The string comparison is nothing but comparing the two input strings to check if both are equal or not. If both strings are equal then the result is true. Otherwise, the result is false.

We can use three standard methods to compare two strings. Those are described below:

  1. Using equals() method,
  2. Using == operator,
  3. Using ComapareTo() method.

Let’s go for the article to see one by one clearly.

String Comparison in Java Using equals() method

Here, The equals() method will help to execute a task.

class Teststringcomparison1{
 public static void main(String args[]){
 
   String s1="code";
   String s2="code";
   String s3=new String("code");
   String s4="coding";

   System.out.println(s1.equals(s2));//true
   System.out.println(s1.equals(s3));//true
   System.out.println(s1.equals(s4));//false
 }
}

Execution

Firstly, We have to declare the four input strings with the respective variables. After executing this program, The equals() method will check whether the 1st string is equal to the 2nd, 3rd, and 4th strings respectively.

If both are equal then it will print the output as “True”. Otherwise, It will express the output as “False”. The system.out.printIn() function will display the output.

Output

The output will show below for your reference.

true
true
false

Using == Opreator

Instead of using the equal() method, We can use the == operator method to perform a task here.

class Teststringcomparison3{
 public static void main(String args[]){
 
   String s1="Codingdiksha";
   String s2="Codingdiksha";
   String s3=new String("Codingdiksha");

   System.out.println(s1==s2);//true (because both refer to same instance)
   System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
 }
}

Explanation

The == operator will use to compare the 1st string is equal to the 2nd and the 3rd string separately after declaring the inputs and variables. Here, The 1st and the 2nd strings are equal so the result is true.

The 1st and the 3rd strings are not equal because the 3rd string refers to the instance created in non-pool so the result is false. Finally, The result will print on the screen using the system.out.println() function.

Output

Below, You can see the reference output of String Comparison in Java.

true
false
Read Also
String Comparison in JavaScript
String Comparison in PHP
String Comparison in Python

Using CompareTo() method

We can do a task using the CompareTo() method rather than all the above methods.

class Teststringcomparison4{
 public static void main(String args[]){
 
   String s1="Coding";
   String s2="Coding";
   String s3="Program";

   System.out.println(s1.compareTo(s2));//0
   System.out.println(s1.compareTo(s3));//1(because s1>s3)
   System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
 }
}

Execution

After assigning the inputs and variables, The values of the strings will be compared using the CompareTo() method. If both string values are equal then the result is “0”. If the 1st string value is greater than the 2nd string value then the result is a positive number.

The result will be a negative number if the 1st string value is less than the 2nd string value. Finally, The system.out.printIn() function will show the output on the screen.

Output

The output of String Comparison in Java will be provided below for your reference.

0
-13
13

Final Words

I hope this article helps you to implement the string comparison in Java Program. If you face any issues please let me know via the comment section. Share this article with other Java program developers via social networks.

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply