• 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
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

how can you modify a String?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to my teacher, String is like an array, it can not be resized once created. But how come you can modify a String like:
String s = "abc";
s += "-" + "def";
This is confusing me.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're actually creating a new String object!
pb
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot modify a String. Strings are by definition immutable. However, you can assign a new String to the same String reference.

It is perfectly legal to write
String s1 ;
String s2 = "Hello";
s1 = "abc" ;
s1 = "def" ;
s1 = s2 ; // s1 is now a reference to the String "Hello"
s1 = "abc" + s2 ;
s1 += s2 ;

When you finish you will have 2 references (s1 and s2) and 5 Strings:
"Hello"
"abc"
"def"
"abcHello"
"abcHelloHello"

The old Strings don't get changed or go away, they just aren't referenced anymore.
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marilyn deQueiroz:
The old Strings don't get changed or go away, they just aren't referenced anymore.


Can you "re-reference" them without assigning the same value again? ie. s1 = "abc"
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once the reference to the String has been lost there is no way to recall it.

However, since the String is still there, when you assign a new (identical) String, Java goes to the String pool, pulls it out , and uses it rather than creating a new identical String.
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pretty clever, where do you learn all this stuff by the way?
After posting the above I realized that my reply could be misinterpret I meant Java is pretty clever, but dont worry Marilyn I think you are too
[This message has been edited by Johannes de Jong (edited April 24, 2001).]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I learned a lot of it in the forums here at JavaRanch (especially about Strings).
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe time that I started "surfing" in the other Forums too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic