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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Compare two Strings in Java
- Compare Dates in Java
- Java Integer compare() method
- How compare() method works in Java
- Compare two strings lexicographically in Java
- Short compare() method in Java
- How to Initialize and Compare Strings in Java?
- RuleBasedCollator compare() method in Java with Example
- Boolean compare() method in Java with Examples
- Byte compare() method in Java with examples
- Float compare() Method in Java with Examples
- Double compare() Method in Java with Examples
- Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()
- Java Guava | Booleans.compare() method with Examples
- Java Guava | Ints.compare() method with Examples
- Java Guava | Longs.compare() method with Examples
- Java Guava | Floats.compare() method with Examples
- Java Guava | Chars.compare() method with Examples
- Java Guava | Doubles.compare() method with Examples
- Java Guava | compare() method of Short Class with Examples