Press "Enter" to skip to content

Comparing Objects in Java – “==”, “equals()”, “compareTo()” and “compare()

Java has a few different ways of comparing variables and objects with one another.

“==”, “equals()”, “compareTo()” and “compare()”.

Java provides various operators such as ‘==’ and ‘!=’ which check for equality and inequality respectively. They are used in a programming flow to check if certain criteria match or are similar, such as:

On line 8 we check if `someString` is the same (‘==’, known as a reference equal) as the String “test”, which it is, so it returns `true` (in this case).

On line 16 we check if `someString2` is equal to the String “test” by using the String’s built in `.equals` method. This is for example, the recommended method of comparing Strings.

The function checks the actual contents of the string, the `==` operator checks whether the references to the objects are equal. (Alnitak, 2009)

Our reference equals usage above compares the objects’ references to see if they point to the same object. This means that they don’t actually check to see if the end values are equal, potentially returning a false positive in some instances.

A reference equals is very useful for primitive data types such as `int`s but soon becomes a problem when comparing more complex objects. Even in our example above we had to use `.equals` to compare the actual value of the variables as opposed to the reference types.

Another useful comparison method is `compareTo`, which performs a lexicographical comparison of two objects. It is specifically useful for sorting comparisons passed to it as the comparison is done by comparing a range of String characters and or Double values.

For example, (String) “abc” is `-1` when compared to (String) “abd” because the “c” is lexicographically one less than “d”. Therefore two strings with the same value would return a `0` as they are the same and have `0` differences. The order of this check is therefore extremely important.

`compareTo` is part of the `Comparable Interface`.

The `compare()` is another method that is useful for comparing objects, it is different to `compareTo()` in that it can sort multiple elements and returns the value instead of modifying the original class.


`compare` is part of the `Comparator Interface`.

Comparable and Comparator are both Interfaces and can both be used to sort collections of elements.

We will highlight some key differences between the two.

Comparable Comparator
Sorting on a single element Sorting on multiple elements
Modifies the original class Result is passed back and original class is not modified
Invoked by means of compareTo() method Invoked by means of compare() method
Part of the java.lang package Part of the java.util package

 

References:

“Java String.equals versus ==” (2009) – Available from: http://stackoverflow.com/questions/767372/java-string-equals-versus (Accessed on 9th April 2017)

“String Class – intern” (n.d.) – Available from: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#intern%28%29 (Accessed on 9th April 2017)

“Interface Comparable<T>” (n.d.) – Available from: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html (Accessed on 9th April 2017)

“Generics in the Java Programming Language” (2004) – Available from: http://www.cs.unibo.it/martini/PP/generics-tutorial.pdf (Accessed on 9th April 2017)

“==, .equals(), compareTo(), and compare()” (2007) – Available from: http://perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html (Accessed on 9th April 2017)