Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

String Class is not Immutable

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you say that strings are immutable
when you can do an operation like
String motto="Program Once"
motto=motto+"execute everywhere"
motto=motto.concat("dont bet oin it!");
(Plz refer Mughal and rasmussen page 311)
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And everyone of those creates a completely new and distinct object. You haven't created one String object... you have created three!
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rama Krishna Pathangi:
How can you say that strings are immutable
when you can do an operation like
String motto="Program Once"
motto=motto+"execute everywhere"
motto=motto.concat("dont bet oin it!");
(Plz refer Mughal and rasmussen page 311)


Because none of these operations modify a String object. Rather, each and every one of these creates a brand new String object. Take this example:

This code snippet generates the following output:

If the original String object had actually been modified, both variables would have referenced the same String. Rather, when the concat method was invoked, a new String was created and returned from that method.
The String object is, indeed, immutable.
Corey
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods on String objects that seems to modify them, don't do that. They just create a new String and return it. The original string object is untouched. If you assign the returned string object to the same variable, that variable just point another object. The original string didn't change.
It is the same for the + operator.
Take a look at this code:
 
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

Originally posted by Thomas Paul:
And everyone of those creates a completely new and distinct object. You haven't created one String object... you have created three!


Hence the reason why String Concatenation is such an expensive operation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic