In Java, evaluating two strings is a frequently encountered activity in contexts such as file verification, user validation, form submission analysis, and database interactions. Java offers various techniques for string comparison, one of which is the == operator.
This article will explore how we can evaluate two strings utilizing the == operator in Java.
Table of Contents:
- What is the == Operator in Java?
- Illustrations for Comparing Strings with the == Operator
- Illustration 1: Evaluating Strings Using the == Operator
- Illustration 2: Comparing String Instances Created Using the new Keyword
- Illustration 3: Comparing String Literals and Instances
- Summary
What is the == Operator in Java?
The ‘==’ operator in Java is utilized to compare the reference points (memory addresses) of two strings rather than their actual contents. It determines whether both references lead to the same string object in memory. This operator == is mainly used for comparing primitive data types, whereas .equals() is more suitable for comparing the content of strings.
Syntax:
boolean result = string1 == string2;
Where:
- String1: The first string under evaluation.
- String2: The second string under evaluation.
- Result: A boolean value (true if both references point to the same object, false otherwise).
Return Type: It invariably returns a boolean value, either true or false.
Illustrations for Comparing Strings using the == Operator
Below are some examples to evaluate strings using the == Operator.
Illustration 1: Evaluating Strings Using the == Operator
In the following example, we will employ the == operator to compare two strings in Java:
Output:

Explanation: As both str1 and str2 are string literals located in the string pool, they point to the same memory location, thus making == return true. Conversely, str3 is found in the heap, and comparing it with str1 via == results in false.
Illustration 2: Comparing String Instances Created
“`html
Utilizing the new Keyword
Let’s examine the example below, where we generated string instances utilizing the new Keyword:
Result:

Clarification: In the above demonstration, we established two strings through the new keyword. As new String(“Intellipaat”) generates two distinct instances in memory, == yields false, despite the equivalence of string content.
Attention: This is a constraint of the == operator; it solely compares two strings that share the same memory reference.
Instance 3: Comparing String Literals and Instances
In the subsequent example, we will employ string literals and instances to evaluate two strings in Java:
Instance:
Result:

Clarification: In the preceding illustration, str1 is a string literal located in the string pool, whereas str2 is a newly created object in heap memory. Given that they point to distinct memory locations, == yields false.
Summary
In this article, we have discovered how to utilize the == operator for comparing two strings through various examples. We have also examined the constraints of the == operator in Java.
The == operator is predominantly applied to compare primitive data types.
If you aspire to master the Java Programming language, you may check out our Java Course.
Alternative Approaches to Compare Strings in Java
- Employing User-Defined Function
- Using String.equalsIgnoreCase() Method
- Utilizing Objects.equals() Method
- Implementing String.compareTo() Method
- Deploying compareToIgnoreCase() Method
- Using startsWith() Method
- Applying endsWith() Method
The article Using Equality (==) Operator to Compare Strings in Java first appeared on Intellipaat Blog.
```
