This week's book giveaway is in the Design forum.We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!

# String Immutable!

Brian Smith
Ranch Hand
Posts: 232
hi all,
i again ran into a hard-to-understand situation that String objects are immutable.. It says that everytime we use concat, replace,substring...etc operation, the new string will be created.i am wondering what happens to the original string then? is there any way the original string can be tracked? i would appreciate if someone could explain this to me.
thanks.

Ranch Hand
Posts: 1873
well,
if u do,
String s = "maulin" + "vasavada" then maulin is in one string and vasavada is in another and both of them are in String "Literal Pool" and we don't have a way to get reference to it.
if we do,
String s1 = "maulin";
String s = s1 + s2;
then we have a reference to s1 and s2 even after adding them and making string s.
there is one interesting aspect of String that is intern() method of String. if i try to explain that then it would be sth like following...
if u do,
String s1 = new String("maulin") [which is different then String s1= "maulin"; (i fear i won't be able to explain that in a manner somebody else might...)] and u have,
String s1 = new String("maulin");
String s2 = new String("maulin");
then s1 == s2 is false as s1 and s2 are different references right? BUT "maulin" is a constant string hence it is there in String Literal Pool and so "maulin" is one string. so its like,
s1 ----> "maulin" in Literal Poool <---- s2
but the references s1 and s2 are different in itself and has copy of the "maulin".
if u do, s1.intern() == s2.intern() then that would return u true!! u can read
API

regards
maulin

Ranch Hand
Posts: 1873
moderators,
i observe one behavior (or mis-behavior?) in URL UBB tag.
that is- if we put () in the the url then it doesn't work and i get HTML Error that says "Perenthesis in HTML".
so i can't post URL that is directly pointing to a method in the API as i wanted it to point to String.intern() method in above case...
regards
maulin

Joel McNary
Bartender
Posts: 1840

String s = "maulin" + "vasavada" then maulin is in one string and vasavada is in another and both of them are in String "Literal Pool" and we don't have a way to get reference to it.

Just as an aside, the compiler recognizes this situation as a compiler literal and does the concatenation for you at compile time. Only the string "maulinvasavada" would wind up in the literal pool; and since you are assigning that to the String s, you do have a reference to it.

Dirk Schreckmann
Sheriff
Posts: 7023
moderators,
i observe one behavior (or mis-behavior?) in URL UBB tag.
that is- if we put () in the the url then it doesn't work and i get HTML Error that says "Perenthesis in HTML".
so i can't post URL that is directly pointing to a method in the API as i wanted it to point to String.intern() method in above case...
regards
maulin

That's a problem with UBB, our current forum software.

Brian Smith
Ranch Hand
Posts: 232
well,
if u do,
String s = "maulin" + "vasavada" then maulin is in one string and vasavada is in another and both of them are in String "Literal Pool" and we don't have a way to get reference to it.
if we do,
String s1 = "maulin";
String s = s1 + s2;
then we have a reference to s1 and s2 even after adding them and making string s.

Suppose, we do s1 = s1 + "vasa"; what happens?
does it change s1 or create another new s1 with new value?if it creates s1 with new value how can we refer back to s1 with it's original value?
thanks.

Michael Morris
Ranch Hand
Posts: 3451
Originally posted by Namaste Sathi:

Suppose, we do s1 = s1 + "vasa"; what happens?
does it change s1 or create another new s1 with new value?if it creates s1 with new value how can we refer back to s1 with it's original value?
thanks.

The string object that s1 refers to never changes. Remeber that s1 is a reference that can point to any String object. What happens in the above scenario is a StringBuffer is created in which the string that is initially referred to by s1 is concatenated with (added to) the literal string "vasa". Then the toString() method of StringBuffer returns a new String object which is assigned to s1.

Brian Smith
Ranch Hand
Posts: 232
hey Michael,
Originally posted by Michael Morris:

What happens in the above scenario is a StringBuffer is created in which the string that is initially referred to by s1 is concatenated with (added to) the literal string "vasa". Then the toString() method of StringBuffer returns a new String object which is assigned to s1.

i don't think above scenario creates a StringBuffer. i think it might create s1 with new value but i am wondering what happens to the original value of s1.

Michael Morris
Ranch Hand
Posts: 3451
Originally posted by Namaste Sathi:
hey Michael,

i don't think above scenario creates a StringBuffer. i think it might create s1 with new value but i am wondering what happens to the original value of s1.

Anytime you add strings together the compiler creates a StringBuffer to handle the concatenation. Let's make a simple program:

Here's the output:

That shows that the original String object that we initially assigned to s1 did not change and did not disappear. It also shows that s1 is now pointing to a different String object and since we know that the original string has not changed we can only conclude that a new String was created in the process.
Now to show you that a StringBuffer is used in this example here is the decompiled class file: