I am a bit confused about assignment operations in Java. After changing y[]
and 'a', x[]
changes together with y[] while b remains as it is. Could somebody clarify the mechanism of "pass by value of the reference of a variable"?
class numTest {
public static void main(String[] args) {
int x[] = new int[]{1, 2, 3};
int y[] = x;
double a = 10.0;
double b = a;
int i;
y[2] = 100; // substitute element in array y
for(i = 0; i < x.length; i++)
System.out.print(x[i] + " ");
System.out.println();
for(i = 0; i < y.length; i++)
System.out.print(y[i] + " ");
System.out.println();
a += 1.0; // increment value of a by 1
System.out.println(a);
System.out.println(b);
}
}
Is it because Java treats primitive and reference data types differently?