Back to questions
JuniorTheoryCommon
What does the program print?
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {1, 2};
int[] b = {1, 2};
System.out.println((a == b) + " " + Arrays.equals(a, b));
}
}false true
- Expecting
true true:==on arrays compares references, and two literals{1, 2}create two separate arrays. - Expecting
Arrays.equalsto need the very same array: it compares the length and each element in order, so equal contents givetrue.
- What does
Arrays.equalsreturn for{1, 2}and{2, 1}? - After
int[] c = a;, what doesa == cprint?