What is the difference between == and equals() in Java?
In Java, the operators ==
and equals()
are used for comparing objects. While they may seem similar, they have different functionalities and purposes. Understanding the difference between these two is crucial for working with objects in Java. In this article, we will explore this topic in detail and provide multiple examples to clarify the concepts.
Understanding the == operator
The ==
operator in Java is used to compare the references of two objects. It checks whether two object references point to the same memory location in the heap. In other words, it checks if the two objects are the same instance or not.
Let's consider an example:
// Object 1
String str1 = "Hello";
// Object 2
String str2 = "Hello";
// Object 3
String str3 = new String("Hello");
System.out.println(str1 == str2); // true
System.out.println(str1 == str3); // false
In this example, we have three objects of type String
. The first two objects, str1
and str2
, are assigned the same string literal "Hello". Since string literals are stored in a common pool known as the string constant pool, both objects str1
and str2
point to the same memory location. Therefore, the comparison str1 == str2
returns true
.
On the other hand, the third object, str3
, is created using the new
keyword, which creates a new instance of the string object. As a result, str3
points to a different memory location. Hence, the comparison str1 == str3
returns false
.
Using the equals() method
While the ==
operator compares the references of objects, the equals()
method is used to compare the content or values of objects. The equals()
method is a member of the Object
class and is available in all Java objects. However, it can be overridden in custom classes to provide a specific comparison logic.
Let's continue with our previous example:
System.out.println(str1.equals(str2)); // true
System.out.println(str1.equals(str3)); // true
In this case, both the equals()
comparisons return true
. This is because the String
class overrides the equals()
method to compare the content of two strings rather than their memory references.
It is important to note that if you are working with custom classes, you need to override the equals()
method to define your own comparison logic. By default, the equals()
method in the Object
class uses the ==
operator to compare memory addresses, which is not suitable for most custom classes.
Summary
To summarize, the ==
operator in Java compares the memory references of two objects, while the equals()
method compares the values or content of objects. The ==
operator is used for reference comparison, whereas the equals()
method provides a way to customize object comparison behavior.
It is recommended to use the equals()
method for object comparison, especially when working with custom classes. This ensures that the comparison logic is defined according to the specific needs of the application.
Remember that the ==
operator can still be useful in certain scenarios, such as checking if an object reference is null
.
Conclusion
In this article, we have explored the difference between the ==
operator and the equals()
method in Java. We have learned that ==
compares the memory references of objects, while equals()
compares their values or content. We have also seen how to use these operators with examples.
It is essential to have a clear understanding of these concepts to write efficient and bug-free code when working with objects in Java.