Both == operator and equals method used to compare to object or primitive variables. By default both check for referential equality which involves checking if do references are pointing to the same object or not. For primitive variables, the value of the variables is compared and appropriate result is returned. == and equals method return a boolean value after comparing the two operands.

Default behavior of equals

While == operator is implemented within the JVM, equals method is defined in the Object class. If we look at the implementation of equals method in the object class we shall see the following code:

    public boolean equals(Object obj) {
        return (this == obj);
    }

Thus we can clearly see that the default implementation of equals method uses == operator for comparing the current object and the argument object.

equals method in String class

However, any class can override the equals method and provide its own implementation code. A classic example of equals method being overridden is the string class. Let us now the code for equals method which is present in the string class:

    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = count;
            if (n == anotherString.count) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = offset;
                int j = anotherString.offset;
                while (n-- != 0) {
                    if (v1[i++] != v2[j++])
                        return false;
                }
                return true;
            }
        }
        return false;
    }

In addition to equals method, the string class also defines a method name as equalsIgnoreCase().

By providing such an implementation of equals method, classes can provide logical equality test instead of referential equality test which is performed by equals method by default.

However there are many classes for which overriding the equals method does not make any sense. Such classes include the string buffer and string builder classes because they do not represent any state data.

Best Practices for equals method

Another best practices related to overriding the equals includes overriding the hash and compareTo methods. Some of the tips which you can use while overriding the equals method include:
1) check for null argument.
2) first test for more significant properties of the class.
3) use the length of an object, if available.
4) since the equals method needs to accept an argument of type Object, use the instanceOf operator before proceeding with testing member variables.

Difference between == operator and equals method in Java admin Core Java
Both == operator and equals method used to compare to object or primitive variables. By default both check for referential equality which involves checking if do references are pointing to the same object or not. For primitive variables, the value of the variables is compared and appropriate result is...
Both == operator and equals method used to compare to object or primitive variables. By default both check for referential equality which involves checking if do references are pointing to the same object or not. For primitive variables, the value of the variables is compared and appropriate result is returned. == and equals method return a boolean value after comparing the two operands. <h2>Default behavior of equals</h2> While == operator is implemented within the JVM, equals method is defined in the Object class. If we look at the implementation of equals method in the object class we shall see the following code: 1 Thus we can clearly see that the <strong>default implementation of equals method uses == operator</strong> for <a href="http://www.javaexperience.com/compare-objects-using-comparator-in-java/" title="compare objects using Comparator in Java">comparing the current object</a> and the argument object. <h2>equals method in String class</h2> However, any class can override the equals method and provide its own implementation code. A classic example of equals method being overridden is the string class. Let us now the code for equals method which is present in the string class: 1 In addition to equals method, the string class also defines a method name as equalsIgnoreCase(). By providing such an <strong>implementation of equals method</strong>, classes can provide logical equality test instead of referential equality test which is performed by equals method by default. However there are many classes for which <strong>overriding the equals method</strong> does not make any sense. Such classes include the string buffer and string builder classes because they do not represent any state data. <h2>Best Practices for equals method</h2> Another best practices related to overriding the equals includes overriding the hash and <a href="http://www.javaexperience.com/overriding-compareto-method-in-java/" title="Overriding compareTo() method in Java">compareTo methods</a>. Some of the tips which you can use while overriding the equals method include: 1) check for <a href="http://www.javaexperience.com/java-null-value-and-nullpointerexception/" title="null value and NullPointerException">null argument</a>. 2) first test for more significant properties of the class. 3) use the length of an object, if available. 4) since the equals method needs to accept an argument of type Object, <strong>use the instanceOf operator</strong> before proceeding with testing member variables.
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Latest posts by admin (see all)