wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

Vasantha Ajjampudi
Greenhorn

Joined: Jul 08, 2002
Posts: 5
I know that Strings are immutable. But practically that is not the case.I am able to change the original String.
If I assign another String value to the original
String , the original String is changed to new one. why is it so?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
No, actually what happens is that the changes you made are foisted on a new String which is created. See the sample code below:

If trim() really trimmed the String of s1 and returned the same object to the String reference s2, then s1 and s2 should be pointing to the same object, which is not the case. Also if we printed out s1 and s2, s1 still has some spaces left over...
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Vasantha,
So, what you've discovered is that, while you may not be able to change a given object, you can change which object a non-final identifier refers to.
AND...
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Vasantha Ajjampudi
Greenhorn

Joined: Jul 08, 2002
Posts: 5
thanks Antony for ur information.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
you're welcome
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

just as a note on performance -- realize that string manipulation/creation is a VERY EXPENSIVE operation. Use StringBuffer if you're doing a lot of concatenation / manipulation.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Yes. Creating objects with unseemly abandon may cause object churn.
Matt Kidd
Ranch Hand

Joined: Jul 17, 2002
Posts: 261
I wonder if at any moment two people are thinking the exact same thought...

Anyway, is there such a distinction as between a String object and a String literal or are they basically one in the same?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Sorry, I was just rereading that boy wanna meet girl thread and I suppose my subconscious is playing tricks on me again
Anyway....
String literals are pooled, in the sense that object references with the same literal "value" refer to the same object. Please see the sample code below:

More info can be found in the JLS in 3.10.5 String Literals. See also the description of the intern() method in the JDK 1.4 docs.
[ September 06, 2002: Message edited by: Anthony Villanueva ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings