Using Equality (==) Operator to Compare Strings in Java


In Java, comparing two strings is the most common task in situations like file validation, user authentication, form input processing, and database queries. Java provides multiple methods to compare strings, one of which is the == operator.

In this blog, we will learn how we can compare two strings with the help of the == operator in Java.

Table of Contents:

What is == Operator in Java?

The ‘==’ operator in Java is used to compare the references (memory locations) of two strings, not their actual value. It checks whether both references point to the same string object in memory. This operator  == is primarily used for comparing primitive data types, while .equals() is preferred for string content comparison.

Syntax:

boolean result = string1 == string2;

Where:

  • String1: The first string to compare.
  • String2: The second string to compare.
  • Result: The boolean value (true if both references point to the same object, otherwise false).

Return Type: It always returns a boolean value either true or false.

Examples to Compare Strings using the == Operator

Here are the following examples to compare strings using the == Operator.

Example 1: Compare Strings using == Operator

In the below example, we will use the == operator to compare two strings in Java:

Output:

Compare Strings using == OperatorCompare Strings using == Operator

Explanation: Since both str1 and str2 are string literals stored in the string pool, they reference the same memory location, making == return true. But str3 is stored in the heap so comparing with str1 using == shows false.

Example 2: Comparing String Objects Created Using new Keyword

Let’s have a look at the below example, in which we created string objects using the new Keyword: 

Output:

Comparing String Objects Created Using new KeywordComparing String Objects Created Using new Keyword

Explanation: In the above example, we have created two strings using the new keyword. Since new String(“Intellipaat”) creates two different objects in memory, == returns false, even though the string content is the same.

Note: This is the limitation of the == operator, it only compares two strings that have the same memory reference. 

Example 3: Comparing String Literals and Objects

In the example below, we will use string literals and objects to compare two strings in Java:

Example:

Output:

Comparing String Literals and ObjectsComparing String Literals and Objects

Explanation: In the above example, str1 is a string literal stored in the string pool, while str2 is a new object in heap memory. Since they reference different memory locations, == returns false.

Conclusion

In this blog, we have learned how we can use the == operator to compare two strings with the help of examples. We have also explored the limitations of the == operator in Java.

The == operator is mainly used to compare primitive data types.

If you want to be an expert in Java Programming language, you may refer to our Java Course.

Some Other Methods to Compare Strings in Java

  • Using User-Defined Function
  • Using String.equalsIgnoreCase() Method
  • Using Objects.equals() Method
  • Using String.compareTo() Method
  • Using compareToIgnoreCase() Method
  • Using startsWith() Method
  • Using endsWith() Method

Leave a Reply

Your email address will not be published. Required fields are marked *