Table of contents
1.
Introduction
2.
 What is == in Java?
2.1.
Java
3.
What is .equals() Method in Java?
3.1.
Java
3.2.
Java
4.
Difference between “ ==” and .equals() in Java
5.
 
6.
 
7.
 
8.
 
9.
 
10.
 
11.
When to Use == and When to Use .equals() in Java
11.1.
Use of ==
11.2.
Use of .equals()
11.3.
Best Practices
11.4.
Common Scenarios and Use Cases
12.
Frequently Asked Questions
12.1.
Can I use == to compare string contents in Java?
12.2.
Is the equals() method case-sensitive?
12.3.
When should I use == for string comparison in Java?
13.
Conclusion
Last Updated: Jun 9, 2025
Easy

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.

 What is == in Java?

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.

What is .equals() Method in Java?

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

 

 

 

 

 

 

 

When to Use == and When to Use .equals() in Java

In Java, comparing strings (or objects in general) can be confusing for beginners due to the difference between == and .equals(). Understanding the right usage is crucial to avoid logical errors.

Use of ==

The == operator compares object references, not their actual content. This means it checks whether two variables point to the same memory location.

Example:

String s1 = "Java";
String s2 = "Java";
System.out.println(s1 == s2); // true
You can also try this code with Online Java Compiler
Run Code

In the example above, both strings refer to the same memory object in the string pool, so == returns true.

However, if strings are created using the new keyword:

String s3 = new String("Java");
System.out.println(s1 == s3); // false
You can also try this code with Online Java Compiler
Run Code

Here, == returns false because s3 refers to a new memory location, even though the content is the same.

Best Use Case: Use == when you want to check if two references point to the exact same object (e.g., singletons or cached instances).

Use of .equals()

The .equals() method is used to compare the actual content of two objects. For strings, this means comparing each character.

Example:

String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1.equals(s2)); // true
You can also try this code with Online Java Compiler
Run Code

This returns true because the content of the two strings is the same.

Best Use Case: Use .equals() when you want to check if two strings have the same value, such as comparing user input, file paths, or names.

Best Practices

  • Always use .equals() for content comparison.
     
  • To avoid NullPointerException, consider using:
     
  • rather than userInput.equals("Java").
     
  • Be cautious when using == for strings created dynamically (e.g., user input), as it may lead to incorrect results.

Common Scenarios and Use Cases

ScenarioRecommended Approach
Comparing string literals== or .equals()
Comparing user input values.equals()
Checking object identity==
Comparing string content.equals()


Use == for reference checks and .equals() for content comparison. Following this guideline ensures accurate and error-free string handling in Java applications.

"Java".equals(userInput);
You can also try this code with Online Java Compiler
Run Code

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.

Live masterclass