Trying to fully grasp Java's pass-by-value. Let's say we have this code:
public class Test {
static void switchIt(Test t) {
t = new Test();
}
public static void main(String ... args) {
Test a = new Test();
switchIt(a);
}
}
When the object referenced by a
gets passed to switchIt()
, the reference value is copied to t
. So we'd have two different reference variables, with identical bit-patterns that point to a single object on the heap.
When t = new Test()
runs, obviously a
still refers to the old object, and t
now points to a new object on the heap. Since the a
and t
reference variables used to have identical bit-patterns, does this mean that Java implicitly changed the bit-pattern of the t
reference variable? Or is it wrong to assume that the bit patterns were ever identical to begin with?
Let's say the a
reference variable is represented on the stack as 0001. When I pass it to the function, that means t
is also represented on the stack as 0001, since I passed a copy of the bits in the reference variable.
When I assign t
to a new Test()
, if t
and a
both are represented as 0001 on the stack, would that 0001 change for t
?