What will be the output of the following java program
class Test { public static void main (String[] args) { int arr1[] = {1, 2, 3}; int arr2[] = {1, 2, 3}; if (arr1 == arr2) // Same as arr1.equals(arr2) System.out.println("Same"); else System.out.println("Not same"); } }
Output:
Not Same
In Java, arrays are first-class objects. In the above program, arr1 and arr2 are two references to two different objects. Therefore, when we compare arr1 and arr2, we compare the two reference variables, so the output we get is "Not Same" (see here for more examples).
How to compare array contents?
An easy way is to run a loop and compare the elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. In fact, the Arrays class has a list of equals() methods for different primitive types (int, char, .., etc.) and a list for object types (which are the basis of all classes in Java).
// we need to import java.util.Arrays to use Arrays.equals(). import java.util.Arrays; class Test { public static void main (String[] args) { int arr1[] = {1, 2, 3}; int arr2[] = {1, 2, 3}; if (Arrays.equals(arr1, arr2)) System.out.println("Same"); else System.out.println("Not same"); } }
Output:
Same
How to Deep compare array contents?
As shown above, Arrays.equals() works fine and compares the contents of the arrays. The question now is what to do if the array contains an array or some other references that refer to different objects but have the same value. For example, refer to the following procedure.
import java.util.Arrays; class Test { public static void main (String[] args) { // inarr1 and inarr2 have same values int inarr1[] = {1, 2, 3}; int inarr2[] = {1, 2, 3}; Object[] arr1 = {inarr1}; // arr1 contains only one element Object[] arr2 = {inarr2}; // arr2 also contains only one element if (Arrays.equals(arr1, arr2)) System.out.println("Same"); else System.out.println("Not same"); } }
Output:
Not Same
So Arrays.equals() is not able to do deep comparison. Java provides another method for this Arrays.deepEquals() which does deep comparison.
import java.util.Arrays; class Test { public static void main (String[] args) { int inarr1[] = {1, 2, 3}; int inarr2[] = {1, 2, 3}; Object[] arr1 = {inarr1}; // arr1 contains only one element Object[] arr2 = {inarr2}; // arr2 also contains only one element if (Arrays.deepEquals(arr1, arr2)) System.out.println("Same"); else System.out.println("Not same"); } }
Output:
Same
How does Arrays.deepEquals() work?
It compares two objects using any custom equals() method they may have (if they have an equals() method other than Object.equals()). If not, this method will continue to compare objects field by field in a recursive manner. When it encounters each field, it will try to use the derived equals() (if it exists), otherwise it will continue to recurse further. This method is suitable for circular object graphs like this: A->B->C->A. It has loop detection, so any two objects can be compared and never enter an infinite loop (source: https://code.google.com/p/deep-equals/).
Exercise: Predict the output of following program
import java.util.Arrays; class Test { public static void main (String[] args) { int inarr1[] = {1, 2, 3}; int inarr2[] = {1, 2, 3}; Object[] arr1 = {inarr1}; // arr1 contains only one element Object[] arr2 = {inarr2}; // arr2 also contains only one element Object[] outarr1 = {arr1}; // outarr1 contains only one element Object[] outarr2 = {arr2}; // outarr2 also contains only one element if (Arrays.deepEquals(outarr1, outarr2)) System.out.println("Same"); else System.out.println("Not same"); } }
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.