Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings

 
Vasantha Ajjampudi
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Vasantha Ajjampudi
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Antony for ur information.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're welcome
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Creating objects with unseemly abandon may cause object churn.
 
Matt Kidd
Ranch Hand
Posts: 267
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic