Leave a comment

Concept of Java : Difference between ‘==’ and equals

In Java, we compare two values using either ‘==’ or equals. Let’s see the difference between these two in this article.

Before explaining the difference, I would like to explain what happens when we assign one value to another – for example, whey you say i = j where i might be a variable of primitive data type or a reference variable. Basically, the bit representation of the value at right hand side, gets copied to the variable at left hand side. If we say i = 2, the bit representation of 2 will get copied to i. if we say i = j, bit representation of the value stored in j will get copied to i.

Now, let’s take an example of Person class:

Class Person{......}
Person p1 = new Person();
Person p2 = p1;

In first statement here, we are creating an object of type Person, and then we are assigning that object to p1 reference variable. Actually, the bit representation of that location where the object of class Person got created, will get copied to p1. And, when we say p2 = p1, whatever is the bit representation stored in p1, gets copied to p2. That’s how values get passed in Java and this is the reason Java is always pass by value.

Now, let’s get back to our original discussion.

  • When we compare values using == operator:
    It just checks whether the bit representations of both the values are same or not.
    For example:
    if (i == j) { //do something }
    Here, JVM will just check if the bit representation of both the things are same or not.
  • When we compare things using equals() method:
    Method equals is written to check if the two values are meaningfully equivalent or not.
    For example:
    You have two objects of a class Person and each Person object has a unique social security number so, you can say that if both the objects of person has same security number, logically, both persons will be same. Although, both the objects are different but they are meaningfully same. Let’s take another example. In a company there are employees and departments. Each employee has an id and a department. Id is unique within a department. So you can always say that if id and department of two employees are same, actually both the employees are same but objects are different. This is called meaningfully equivalent.

    How do we compare using equals?

    class Person { int socialSecurityNumber; }

    Suppose, you have two objects p1 and p2:

    if ( p1.equals( p2 ) ) { // do something }

    Method equals() is defined in Object class to just check that if both the objects being compared have same bit representation. However, as the basic intent of the equals() method is to check if two objects are meaningfully equivalent, you need to override it with required definition for your class, e.g.:

    public boolean equals(Object obj) {
        if(obj instanceof Person) {
            return this.socialSecurityNumber = = ((Person)obj).socialSecurityNumber;
        return false;

    You can check all the criteria, whatever you want, inside equals method to check if two objects are meaningfully equivalent.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: