Predict the output of following Java program.
In Java, arrays are first class objects. In the above program, arr1 and arr2 are two references to two different objects. So when we compare arr1 and arr2, two reference variables are compared, therefore we get the output as “Not Same” (See this for more examples).
How to compare array contents?
A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. Actually, there is a list of equals() methods in Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is base of all classes in Java).
How to Deep compare array contents?
As seen above, the Arrays.equals() works fine and compares arrays contents. Now the questions, what if the arrays contain arrays inside them or some other references which refer to different object but have same values. For example, see the following program.
So Arrays.equals() is not able to do deep comparison. Java provides another method for this Arrays.deepEquals() which does deep comparison.
How does Arrays.deepEquals() work?
It compares two objects using any custom equals() methods they may have (if they have an equals() method implemented other than Object.equals()). If not, this method will then proceed to compare the objects field by field, recursively. As each field is encountered, it will attempt to use the derived equals() if it exists, otherwise it will continue to recurse further.
This method works on a cyclic Object graph like this: A->B->C->A. It has cycle detection so ANY two objects can be compared, and it will never enter into an endless loop (Source: https://code.google.com/p/deep-equals/).
Exercise: Predict the output of following program
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Compare two strings lexicographically in Java
- Compare two Strings in Java
- How to Compare two Collections in Java?
- C program to Compare Two Strings without using strcmp() function
- Java Integer compare() method
- Short compare() method in Java
- Boolean compare() method in Java with Examples
- Byte compare() method in Java with examples
- Compare Dates in Java
- Float compare() Method in Java with Examples
- Double compare() Method in Java with Examples
- How compare() method works in Java
- Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()
- Java Guava | Booleans.compare() method with Examples
- Java Guava | Ints.compare() method with Examples
- Java Guava | compare() method of Short Class with Examples
- Java Guava | Floats.compare() method with Examples
- Java Guava | Longs.compare() method with Examples
- Java Guava | Chars.compare() method with Examples
- Java Guava | Doubles.compare() method with Examples