Consider this piece of code:
String s = "abc"; m(s); System.out.println(s);
The last line prints not the expected abc
but something different, for example dbc
. How does m()
do it?
Consider this piece of code:
String s = "abc"; m(s); System.out.println(s);
The last line prints not the expected abc
but something different, for example dbc
. How does m()
do it?
July 2, 2012 at 06:26
I give up, how do you do it? It should not be possible since String is immutable and there are no methods in String to alter it. I imagine you would have to somehow get the address location of s and have it point to a new String. Maybe you can email me the solution, I am very curious.
July 5, 2012 at 23:30
Jose, you are right
String
is immutable you can’t change the value stored in it after the object is created. Yet there’s a way to do it…– it’s reflection.See the complete solution in Is it possible to change a String?.
July 5, 2012 at 23:17
[…] was my answer to the question How can the value of string be changed? I was asked this question on a job […]