Why next code works like it uses reference types rather than primitive types?
int[] a = new int[5];
int[] b = a;
a[0] = 1;
b[0] = 2;
a[1] = 1;
b[1] = 3;
System.out.println(a[0]);
System.out.println(b[0]);
System.out.println(a[1]);
System.out.println(b[1]);
And the output is:
2
2
3
3
rather than
1
2
1
3
The contents of the int array may not be references, but the int[] variables are. By setting
b = ayou’re copying the reference and the two arrays are pointing to the same chunk of memory.