String a = "test";
String b = a;
a = "wuut";
System.out.println(b);
Prints out test
Shouldn’t b hold refence of a, not just take its value?
Doesn’t Java work that way with objects and stuff?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When
ais created lets say it point to placememory_1in memory.When
bis assigned toa, thenbalso points to the samememory_1location.Now, when
achanges value (and because the String Object is immutable) a new value is created now in memory and nowapoints inmemory_2.But hey,
bstill points inmemory_1.PS: Immutability is: