• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Strings

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Antony for ur information.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you're welcome
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Creating objects with unseemly abandon may cause object churn.
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic