A little confused about object reference in Java. As I understand it, when I say:
Obj objA = new Obj();
Obj objB = objA;
I'm assigning objB a reference to objA. So if I change either I'll change the other. This is in fact how it works with Arrays. Say I pass Boolean[] a = {false, false}
to the following method:
Boolean[] test(Boolean[] a){
Boolean[] b = a; //Array object = Array object
b[0] = true;
System.out.println("In method:");
for(int i=0;i<a.length;i++){
System.out.printf("%d ", a[i]?1:0);
}
System.out.println();
return a;
}
On return, a[0]
is now true. But say instead I pass the same thing to:
Boolean[] test2(Boolean[] a){
Boolean[] b = new Boolean[2];
b[0] = a[0]; //Boolean obj = Boolean obj
b[0] = true;
System.out.println("In method:");
for(int i=0;i<a.length;i++){
System.out.printf("%d ", a[i]?1:0);
}
System.out.println();
return a;
}
If I return a now, a[0]
will still be false. (It works the same if I say b = new Boolean(true);
) But what's the difference? In both cases I said Object = Object. Why does the reference work in the first but not in the second? Furthermore, is there a way to get the same functionality where one cell refers to another the way one array refers to another?