The Strings are used to store the text in Java programming. Strings are considered immutable objects and they can not be changed or modified. There are various methods present in the String class that can be applied according to the need. In certain scenarios, there is a need to implement a method that compares the strings with each other to obtain the desired result. The comparison between the strings in Java is performed using different ways. These are the equalsTo(), the compareTo() method, and the equality operator(==).
In this article, we will discuss in detail the implementation of the equality operator to compare the strings in Java.
How to Compare Strings Using Equality Operator in Java?
The equality operator is used to compare the references not the values of the declared Strings and it returns the output when both the strings basically match with each other. If the same string corresponds to two different objects the resultant output is always false. Since this operator always uses references to compare the values.
Below is the syntax of the equality operator and then the code implementation.
LHS value == RHS value
Example 1: Compare Two Strings Using Equality Operator
The code below depicts that the integer and the string are compared using the equality(==) operator.
import java.io.*;
public class equalityoperator {
public static void main(String[] args)
{
//Declare the integer and strings for comparison
int a = 10;
int b = 15;
String x="Java";
String y="Programming";
String z="Java";
//Compare a and b using the equality operator
System.out.println("Are " + a + " and " + b
+ " equal? " + (a == b));
//Compare x and z using the equality operator
System.out.println("Are " + x + " and " + z
+ " equal? " + (x == z));
}
}
In the above code:
- The required package of Java is imported.
- A class is declared as an “equalityoperator” to implement the equality operator in Java.
- The Integers and Strings are declared for comparison.
- The strings and the integers are compared using the equality(==) operator and the output is printed using the println() method.
Output
The below output depicts that when 10 and 15 are compared, the equality operator returns false. Whereas when the strings “y” and “z” are compared, the equality operator returns true since both the strings are equal to each other.

Example 2: Creating Different Objects for the Same String
The code below depicts the implementation of the equality operator when two different objects are created and exactly the same string is passed for comparison.
public class Books {
public static void main(String[] args) {
//Declare two objects with the same string
String bookone = new String("Book 1");
String booktwo = new String("Book 1");
//If else statement to pass the equality operator for comparison
if (bookone == booktwo) {
System.out.println("These books are the same.");
} else {
System.out.println("These books are not the same.");
}
}
}
In the above block of code:
- A Class named “Books” is declared for comparison.
- Two string objects bookone and booktwo are created.
- The string “Book 1” is assigned to both objects.
- The two strings declared in the objects are compared using the equality operator.
- The println() method of Java prints the result accordingly.
Output
The output is false since both the strings refer to different objects and the equality operator is using the object as the reference for comparison.

Example 2: Incompatible Data Types
The code below depicts that there are different types used while comparing which eventually yields the error.
//Declare a class to compare the elements from two different data types
class comparison {
public static void main(String[] args)
{
//The first type is thread
Thread t = new Thread();
//The second type is string
String s = new String("PROGRAMMING IN JAVA");
//Comparison between the string and the thread
System.out.println(t==s);
}
}
In the above code block:
- A class for comparison is declared.
- Two different types, string, and thread are declared for comparison.
- The println() method compares the thread and the string and outputs the result.
Output
The output results in an error since thread and string are two different types and can not be compared with each other.

Disadvantage of the Equality Operator
The main disadvantage of the equality operator is that it compares the references, not the actual strings. If multiple objects represent the same string, an error arises which results in a false output. Therefore it is recommended to use another method like the compareTo() method and equals() method for string comparison.
This sums up the implementation of the equality operator in Java.
Conclusion
The equality operator in Java is used to compare references of the string rather than comparing the values declared in the string. The operator throws an error when the same string references different objects. In this article, we have implemented the equality operator to compare the strings in Java.