JavaIntermediate#puzzle#arrays

What is the output of this array comparison? int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; System.out.println(a == b); System.out.println(a.equals(b)); System.out.println(Arrays.equals(a, b));

Arrays don't override equals(), so both a == b and a.equals(b) compare references and are false since they're different array objects. Arrays.equals() compares element-by-element content, so it correctly returns true.

Example
int[] a = {1,2,3}, b = {1,2,3};
System.out.println(a == b);              // false
System.out.println(a.equals(b));         // false
System.out.println(Arrays.equals(a, b)); // true

Related Questions

1
JavaAdvanced#puzzle#oop

What does this output? class Base { int x = 10; int getX() { return x; } } class Derived extends Base { int x = 20; int getX() { return x; } } Base obj = new Derived(); System.out.println(obj.x); System.out.println(obj.getX());

Open
2
JavaAdvanced#jvm

What is the difference between a class loader's parent delegation model levels: Bootstrap, Extension/Platform, and Application?

Open
3
JavaAdvanced#jvm

What is JIT compilation?

Open