I have a bit of trouble understanding the difference between the two:
Let's pretend that:
String x = "Test";
String y = x;
y = "Not Test";
The memory diagram for these two steps is as follows:
- x-> "Test"
- x-> "Test" <-y
- x-> "Test" y-> "Not Test"
But consider class A with the 'int var' field:
A a = new A();
A b = a;
b.var = 5;
Here, if we change b.var, a.var will also change to 5. Why is this different from the above case by lines?
Also, I would appreciate it if someone did not mind explaining these memory diagrams for strings and objects. Will the result be different if a and b are objects of different classes equal? Thank you
source
share