I would extend your example with a third
variable so you can understand better
int[] first = new int[2];
first[0] = 3;
first[1] = 7;
int[] second = new int[2];
second[0] = 3;
second[1] = 7;
int[] third = first;
if (Arrays.equals(first, second))
System.out.println("first && second contain the same elements.");
else
System.out.println("first && second elements are different.");
if (Arrays.equals(first, third))
System.out.println("first && third contain the same elements.");
else
System.out.println("first && third elements are different.");
if (first == second)
System.out.println("first && second point to the same object.");
else
System.out.println("first && second DO NOT point to the same object.");
if (first == third)
System.out.println("first && third point to the same object.");
else
System.out.println("first && third DO NOT point to the same object.");
Output:
first && second contain the same elements.
first && third contain the same elements.
first && second DO NOT point to the same object.
first && third point to the same object.
The ==
equal to operator will only check if they are the same object (and third is an alias to first, so they both point to the same object).
While Arrays.equals(a, b)
will compare all the elements in both arrays, and return true if they all match.