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?

Advertisements

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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: