aspose file tools*
The moose likes Beginning Java and the fly likes String Immutable! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Immutable!" Watch "String Immutable!" New topic
Author

String Immutable!

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
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.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
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 s2 = "vasavada";
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
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
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

Joined: Aug 20, 2001
Posts: 1824

Originally posted by Maulin Vasavada:

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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Originally posted by Maulin Vasavada:
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.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
Originally posted by Maulin Vasavada:
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 s2 = "vasavada";
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

Joined: Jan 30, 2002
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.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Brian Smith
Ranch Hand

Joined: Oct 26, 2002
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

Joined: Jan 30, 2002
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Immutable!