Reference assignment query?
I was wondering why this doesn't work. I thought when I said
val2 = val1 that I was saying that val2 points to the same memory address
as val1 and so my modifying the value of val2 I am also modifying the value of
val1 but I've noticed for Integer and String that the values are not equal when modified.
Expected Value of val1: 11
Integer val1 = new Integer(10);
Integer val2 = val1;
Actual Value of val1: 10
This referencing works for my own objects
Expected Value of p1.x = 1000
Point p1 = new Point(5,10);
Point p2 = p1;
p2.x = 1000;
Actual Value of p1.x = 1000
Can someone please tell me what's so special about Integer and String and why they behave differently? If there's any good explanation about this on the web I would be happy to read.