Do you think IIT Guwahati certified course can help you in your career?
No
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.
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
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
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
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 Type
Compares references
Compares contents
Checks for
Identity (same object in memory)
Equality (same sequence of characters)
Case Sensitivity
N/A (compares references)
Case-sensitive (use equalsIgnoreCase() for case-insensitive comparison)
Usage
Comparing references of string objects
Comparing contents of strings
Example
str1 == str2
str1.equals(str2)
Result for same literal
true (references same object in string pool)
true (contents are equal)
Result for different objects with same content
false (references different objects)
true (contents are equal)
Performance
Faster (compares references)
Slower (compares contents character by character)
Recommended Usage
Checking if two strings are the same object
Checking 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.