While working with Java, you often need to compare values or objects. Java provides two main ways to do this:
- The == operator
- The .equals() method
Sometimes, they might seem the same, but they work differently. The main difference between these two methods is that the == operator checks for reference, while the equals() method checks for content.
In this blog, we’ll explain the difference between == and equals() in Java in simple terms.
Table of Contents:
What is the ‘==’ operator in Java?
The == operator in Java is used to compare memory addresses. It checks if two variables refer to the same object in memory. For primitive data types, == compares actual values, but for objects, it checks the reference of the objects.
Example 1: == operator for primitive data types
Output:
Explanation:
- Since a and b both store 10, a == b returns true.
- Similarly, x == y compares the values 5.5 and 5.5, returning true.
- And 100 == 300 returns false as they are not equal.
Example 2: == operator for objects
Output:
Explanation: Even though s1 and s2 contain the same text, they are stored in different memory locations. Since == compares memory addresses, it returns false.
What is the .equals() method in Java?
The .equals() method is used to compare the actual content of objects, not their memory addresses. By default, the equals() method in the Object class works like ==, but it can be overridden to compare object values instead.
Example:
Output:
Explanation: Here, .equals() compares the text inside s1 and s2. Since both contain “Java”, it returns true.
Java String Pool and Memory
In Java, strings are immutable, meaning their values cannot be changed once created. To optimize memory and improve performance, Java uses a String Pool. The Java String Pool is a special memory area inside the heap where string literals are stored.
Working of Java string Pool
When a new string literal is created, Java checks the String Pool first:
- If the string already exists, the reference to the existing object is returned.
- If the string does not exist, a new object is created in the pool.
This concept helps us to save the memory by not saving duplicate copies of two string objects.
Example:
String s1 = "Hello"; //String Pool
String s2 = "Hello";
System.out.println(s1 == s2); // true
Working of Java Heap Memory
When you use a new String(“…”), Java creates a new object in the Heap, even if the same string exists in the pool. These objects are not shared, leading to different memory locations.
Example:
String s1 = "Hello"; // String pool
String s2 = new String("Hello"); // String Heap
== Operator vs .equals() method in Java
Here are the following differences between == and .equals() in Java:
Feature | == Operator | .equals() Method |
Compares | It is used to compare the reference or memory of the object. | It is used to compare the values of two strings. |
Used for | It is used for primitive types & object references. | This method can’t be used for primitive values. |
Works with | This operator works for all the Java types | It mainly works for Strings. |
Default Behavior | The default behavior of this operator is to compare memory addresses. | The default behavior of this method is to compare references. |
Overriding | This operator cannot be overridden. | This method can be overridden in custom classes. |
Example | str1 == str2 (checks memory) | str1.equals(str2) (checks value) |
Best Practices for Using == and .equals() in Java
Here are the following best practices that you must follow to use == and .equals() in Java:
1. Use == for Primitive Data Types
You should use the == operator for primitive data types such as int, double, char, boolean, etc. You should avoid using the equals() method for primitive data types, as they don’t have methods.
Example:
int a = 10;
int b = 10;
System.out.println(a == b); // true
2. Use .equals() for Comparing Objects
You should use .equals() when comparing object values instead of memory references. You can avoid using the == operator for comparing two objects, if you want to compare two objects by reference only, then you can use the == operator.
Example:
String s1 = new String("Java");
String s2 = new String("Java");
System.out.println(s1.equals(s2)); // true
System.out.println(s1 == s2); // false (different memory locations)
3. Use .equals() for Wrapper Classes (Integer, Double, etc.)
You have to use the .equals() method for wrapper classes like: Integer, Double, etc. You should avoid using the == operator with wrapper classes, as they may not always refer to the same object due to caching.
Example:
Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true (cached values)
System.out.println(a.equals(b)); // true
Integer x = 128;
Integer y = 128;
System.out.println(x == y); // false (different objects)
System.out.println(x.equals(y)); // true
4. Avoid Using == for String Comparison
You should not use the == operator for comparing strings, as it checks memory location, which may lead to incorrect results. Therefore, always use .equals() for comparing strings.
Example:
String s1 = "hello";
String s2 = new String("hello");
System.out.println(s1.equals(s2)); // true
System.out.println(s1 == s2); // false
Conclusion
So far in this blog, we have learned the difference between the == operator and the equals() method in Java. The == operator checks if two variables refer to the same object in memory. While the .equals() method checks the actual content of objects. When working with strings, always use .equals() for comparison to avoid unexpected results.
If you want to learn more about Java, kindly refer to our Java Course.
Difference between == and equals() in Java – FAQs
Q1. What is the difference between str1 == str2 and str1.equals(str2)?
- str1 == str2 checks if both variables point to the same object in memory.
- str1.equals(str2) checks if the content inside both strings is the same.
Q2. What is the difference between == and .equals() in string comparison?
- == checks memory address (useful for primitive data types).
- .equals() checks actual string content (useful for object comparison).
Q3. What are the rules of the .equals() method?
- .equals() is a method available in the Object class.
- It can be overridden by custom classes to compare values instead of memory locations.
- It should be used for comparing objects like Strings, Lists, and custom data types.
Q4. Why is a string immutable in Java?
Strings in Java are immutable for security, memory optimization (String Pool), and thread safety, ensuring values cannot be changed after creation.
Q5. Can .equals() be overridden, and how does it affect comparison?
Yes, .equals() can be overridden to compare objects based on content instead of memory addresses, allowing custom equality checks in user-defined classes.