Arrays.equals and toString
Compare and print arrays.
Comparing and Printing
Arrays do not override equals or toString in a useful way. The Arrays utility class provides equals and toString that actually inspect element contents.
Why == Fails
Using == on arrays compares references, not contents. Two different arrays with identical elements are not == equal.
public class Main {
public static void main(String[] args) {
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b);
}
}All lessons in this course
- Arrays.sort and Sorting
- Arrays.binarySearch
- Arrays.fill and copyOf
- Arrays.equals and toString