Fayçal Salhi
Fayçal Salhi

Reputation: 81

How to compare Object Arrays using Arrays.equals() in java?

in this example showing how to do it :

Student[] stud1 = new Student[2];
Student[] stud2 = new Student[2];
Student[] stud3 = new Student[2];
boolean b;
stud1[0]= new Student("Johnny","Bravo");
stud1[1]= new Student("Ace","Ventura");
stud2[0]= new Student("Ash","Ketchum");
stud2[1]= new Student("Mike","Wazowski");
b = Arrays.equals(stud1,stud2);
System.out.println(b);
stud2 [0] = stud1[0];
stud2[1] = stud1[1];
b = Arrays.equals(stud1,stud2);
System.out.println(b); 
stud3 = stud1;
b = Arrays.equals(stud1,stud3);
System.out.println(b);
}

the result is false true true. What is Arrays.equals comparing exactly? thanks.

Upvotes: 2

Views: 4849

Answers (3)

NewToJava
NewToJava

Reputation: 159

According to javadoc of equals method of Arrays class:

Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if

  (e1==null ? e2==null : e1.equals(e2)).  

In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

Here elements in the array are "java.lang.String" so you should also look into Strings equal method, which says,

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

Directly from the docs:

Returns true if the two specified arrays of Objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). In other words, the two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

That pretty much explains everything. In your case, the first output is false because stud1 and stud2 have instances of Student that are not equal. In the second case, the two arrays have identical objects, and hence test as equal. In the third case, stud1 and stud3 are the same object, hence also equal.

The only interesting case would be if Student had an equals() method and you wanted to test two arrays that had equivalent but not identical Student objects:

Student[] stud1 = {
    new Student("Johnny","Bravo")
};
Student[] stud2 = {
    new Student("Johnny","Bravo")
};
System.out.println(Arrays.equals(stud1, stud2));

Here the output would depend on the implementation of Student.equals().

Upvotes: 4

Hiren Jungi
Hiren Jungi

Reputation: 854

array1.equals(array2) is the same as array1 == array2, i.e. is it the same array.

Arrays.equals(array1, array2) compares the contents of the arrays.

Upvotes: 1

Related Questions