Table of contents
1.
Introduction
2.
Equality Operator (==)
2.1.
Java
3.
Java String equals() Method
3.1.
Java
3.2.
Java
4.
Difference between “ ==” and .equals() in Java
5.
Frequently Asked Questions
5.1.
Can I use == to compare string contents in Java?
5.2.
Is the equals() method case-sensitive?
5.3.
When should I use == for string comparison in Java?
6.
Conclusion
Last Updated: Jul 28, 2024

Difference between Comparing string using == and .equals() Method in Java

Author Sinki Kumari
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Strings are one of the most important components of any programming and when you work with strings in Java, it's important to understand how to compare them properly. Two common ways to compare strings are using the equality operator (==) and the equals() method. While they may seem similar, they serve different purposes and can lead to different results. 

Difference between Comparing string using == and .equals() Method in Java

In this article, we'll learn about the differences between using == and .equals() for string comparison in Java. We'll discuss the basics of each approach, with code examples, and will see when to use each one.

Equality Operator (==)

In Java, the equality operator (==) is used to compare the references of two objects. When applied to strings, it checks if both string variables are referring to the same object in memory. It does not compare the actual contents of the strings.

For example : 

  • Java

Java

String str1 = "Hello";

String str2 = "Hello";

String str3 = new String("Hello");

System.out.println(str1 == str2);

System.out.println(str1 == str3);
You can also try this code with Online Java Compiler
Run Code

 

Output: 

true
false


In the above code, str1 and str2 are both assigned the literal value "Hello". Due to string interning in Java, they refer to the same object in the string pool. Therefore, str1 == str2 evaluates to true.

On the other hand, str3 is created using the new keyword, which explicitly creates a new string object in memory. Even though str1 and str3 have the same content, they refer to different objects. As a result, str1 == str3 evaluates to false.

Java String equals() Method

The equals() method is a built-in method provided by the String class in Java. It is used to compare the contents of two strings for equality. Unlike the == operator, equals() compares the actual characters of the strings rather than their references.

For example : 

  • Java

Java

String str1 = "Hello";

String str2 = "Hello";

String str3 = new String("Hello");

System.out.println(str1.equals(str2));

System.out.println(str1.equals(str3));
You can also try this code with Online Java Compiler
Run Code

 

Output: 

true
true


In the above code, str1.equals(str2) returns true because both strings contain the same sequence of characters, regardless of whether they refer to the same object or not.

Similarly, str1.equals(str3) also returns true because the equals() method compares the contents of the strings, not their references. Even though str3 is created using the new keyword, it still contains the same characters as str1.

It's important to note that the equals() method is case-sensitive. If you want to perform a case-insensitive comparison, you can use the equalsIgnoreCase() method instead.

  • Java

Java

String str4 = "hello";

System.out.println(str1.equals(str4));

System.out.println(str1.equalsIgnoreCase(str4));
You can also try this code with Online Java Compiler
Run Code

 

Output

false
true


In the above example, str1.equals(str4) returns false because the characters are not the same due to the difference in case. However, str1.equalsIgnoreCase(str4) returns true since it performs a case-insensitive comparison.

Difference between “ ==” and .equals() in Java

Parameters

Equality Operator (==)

equals() Method

Comparison TypeCompares referencesCompares contents
Checks forIdentity (same object in memory)Equality (same sequence of characters)
Case SensitivityN/A (compares references)Case-sensitive (use equalsIgnoreCase() for case-insensitive comparison)
UsageComparing references of string objectsComparing contents of strings
Examplestr1 == str2str1.equals(str2)
Result for same literaltrue (references same object in string pool)true (contents are equal)
Result for different objects with same contentfalse (references different objects)true (contents are equal)
PerformanceFaster (compares references)Slower (compares contents character by character)
Recommended UsageChecking if two strings are the same objectChecking if two strings have the same content

 

Frequently Asked Questions

Can I use == to compare string contents in Java?

No, the == operator compares references, not contents. Use the equals() method to compare string contents.

Is the equals() method case-sensitive?

Yes, the equals() method is case-sensitive. For case-insensitive comparison, use the equalsIgnoreCase() method.

When should I use == for string comparison in Java?

Use == when you want to check if two string variables refer to the same object in memory.

Conclusion

In this article, we learned about the differences between comparing strings using the equality operator (==) and the equals() method in Java. We saw that == compares the references of string objects, while equals() compares their contents. While both are useful but It's important to use the appropriate method depending on whether you need to check for object identity or content equality.

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass