Java Program to Differentiate String == operator and equals() method

JavaCampus InterviewServer Side ProgrammingProgramming

In this article, we will understand how to differentiate == operator and equals() method in Java. The == (equal to) operator checks if the values of two operands are equal or not, if yes then condition becomes true.

The equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Below is a demonstration of the same −

Suppose our input is

The first string : abcde
The second string: 12345

The desired output would be

Using == operator to compare the two strings: false
Using equals() to compare the two strings: false

Algorithm

Step 1 – START
Step 2 - Declare two strings namely input_string_1, input_string_2 and two boolean values namely result_1, result_2.
Step 3 - Define the values.
Step 4 - Compare the two strings using == operator and assign the result to result_1.
Step 5 - Compare the two strings using equals() function and assign the result to result_2.
Step 5 - Display the result
Step 6 - Stop

Example 1

Here, we bind all the operations together under the ‘main’ function.

public class compare {
   public static void main(String[] args) {
      String input_string_1 = new String("abcde");
      System.out.println("The first string is defined as: " +input_string_1);
      String input_string_2 = new String("12345");
      System.out.println("The second string is defined as: " +input_string_2);
      boolean result_1 = (input_string_1 == input_string_2);
      System.out.println("\nUsing == operator to compare the two strings: " + result_1);
      boolean result_2 = input_string_1.equals(input_string_2);
      System.out.println("Using equals() to compare the two strings: " + result_2);
   }
}

Output

The first string is defined as: abcde
The second string is defined as: 12345

Using == operator to compare the two strings: false
Using equals() to compare the two strings: false

Example 2

Here, we encapsulate the operations into functions exhibiting object-oriented programming.

public class Demo {
   static void compare(String input_string_1, String input_string_2){
      boolean result_1 = (input_string_1 == input_string_2);
      System.out.println("\nUsing == operator to compare the two strings: " + result_1);
      boolean result_2 = input_string_1.equals(input_string_2);
      System.out.println("Using equals() to compare the two strings: " + result_2);
   }
   public static void main(String[] args) {
      String input_string_1 = new String("abcde");
      System.out.println("The first string is defined as: " +input_string_1);
      String input_string_2 = new String("12345");
      System.out.println("The second string is defined as: " +input_string_2);
      compare(input_string_1, input_string_2);
   }
}

Output

The first string is defined as: abcde
The second string is defined as: 12345

Using == operator to compare the two strings: false
Using equals() to compare the two strings: false
raja
Updated on 29-Mar-2022 12:30:48

Advertisements