Demystifying the Distinction- Understanding the Difference Between `equals()` and `==` in Java

by liuqiyue
0 comment

Difference between `==` and `equals()` in Java

In Java, comparing objects can sometimes be a bit tricky, especially when it comes to understanding the difference between the `==` operator and the `equals()` method. Both are used to compare values, but they serve different purposes and operate in different contexts. This article aims to clarify the distinction between `==` and `equals()` in Java.

Understanding `==` Operator

The `==` operator is a binary operator in Java that checks for equality between two objects. It compares the references of the objects, not their values. In other words, `==` checks if both operands refer to the same object in memory. This operator is often used when comparing primitive data types, such as `int`, `double`, and `char`.

For example, consider the following code snippet:

“`java
Integer a = new Integer(10);
Integer b = new Integer(10);
System.out.println(a == b); // Output: false
“`

In this example, although the values of `a` and `b` are the same, the `==` operator returns `false` because `a` and `b` are referencing different objects in memory.

Understanding `equals()` Method

The `equals()` method, on the other hand, is a method defined in the `Object` class, which is the superclass of all classes in Java. The purpose of the `equals()` method is to compare the values of two objects. By default, the `equals()` method in the `Object` class compares object references, similar to the `==` operator. However, many classes in Java override the `equals()` method to provide value-based comparison.

For example, consider the following code snippet:

“`java
String s1 = new String(“Hello”);
String s2 = new String(“Hello”);
System.out.println(s1.equals(s2)); // Output: true
“`

In this example, the `equals()` method returns `true` because both `s1` and `s2` are referencing the same string object in memory, even though they were created using the `new` keyword.

Overriding `equals()` Method

When a class overrides the `equals()` method, it is typically done to provide a value-based comparison. This is especially important for classes that represent complex data structures, such as `String`, `Integer`, and `Date`. Here’s an example of how the `equals()` method can be overridden:

“`java
class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
Person person = (Person) obj;
return age == person.age && (name != null ? name.equals(person.name) : person.name == null);
}
}
“`

In this example, the `equals()` method is overridden to compare the `name` and `age` fields of two `Person` objects.

Conclusion

In conclusion, the `==` operator compares object references, while the `equals()` method compares object values. It is crucial to understand the difference between these two concepts, especially when dealing with complex data structures in Java. By knowing the distinction, you can avoid potential bugs and ensure that your code behaves as expected.

You may also like