• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Strings and immutability

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For some reason, I'm still not understanding this concept after reading 2 books and trying different things/programs with the JVM.
I create 1 string like this:
String str = "String str";
and another like this:
String str2 = new String("String str");
str += " 1";
str2 += " 2";
And if I print these 2 out, I get:
String str 1
String str 2
I tried concat(); with the above objects too and I can continue to change the contents .. where does the "immutability" of strings come into play? I understand that my above statements (str += " 1"; and str2 += " 2" are returning NEW string objects, thus I'm able to change the contents. What CAN'T I do then to strings? Mughal says Strings are "read-only" once the string has been created and initialized.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok I was just looking over the StringBuffer class .. when I call a method of the StringBuffer class, I call the method directly and do NOT return a new object (since this isn't required by the StringBuffer class). However, if I call a method in the String class on a String object, I *have* to return a new object.
That's it, right?? Is that all immutability is then? Maybe I made the concept harder than it really is?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Eric
what i understand of string immutability is that each time u edit a string a new string object is returned so if u're doing extensive string manipulation u're gonna end up with tons of old string objects taking up memory, very ineffecient, so using the StringBuffer class u can do string manipulation but returns the same object, more effecient!
Please anyone correct me where i'm wrong, {;-)
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tru reading this:
http://www.javaranch.com/ubb/Forum24/HTML/008396.html
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this :
String str=new String("Hello");
str.concat("Everyone");
System.out.println(str);
you will get original string that is "Hello".
but if you write like this :
String str = new String("Hello");
str = str.concat(" Everyone");
System.out.println(str);
this time you will get "Hello Everyone"
You are not changing the original string but you are creating new String object and assigning it to same reference var., original string is immutable.
 
reply
    Bookmark Topic Watch Topic
  • New Topic