Java: changing the value of a String

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?

3 Responses to “Java: changing the value of a String”

  1. Jose Says:

    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.

  2. Is it possible to change a String « Programming is my life Says:

    […] was my answer to the question How can the value of string be changed? I was asked this question on a job […]


Leave a reply to Jose Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.