• 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

String - immutable

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why String are called immutable.
### for this case;We can change the String value
They why it is called immutable.
String a = new String("Raman");
System.out.println(a);
a="kumar";
System.out.println(a);

Regards,
M.S.Raman.
 
Author
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String object are immutable. I think you are confusing the notion of a variable that stores a reference to an object of some kind, and the object itself. Your code fragment creates a new String object:
String a = new String("Raman");
// a now stores a reference to a String object "Raman"
System.out.println(a); // Outputs the original
a="kumar"; // Creates a new String object
// a now contains a reference to a new String object "kumar"
// The old object, "Raman" has been discarded and will be destroyed
System.out.println(a); // Outputs the new one
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strings are immutable to make String.substring() efficient. If they were not immutable, String.substring() would have to copy data because changing the original string might change the substring. As it is now, it can just point to a place within the string.
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is the reference of a string object changes not the string object itself.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This confusion is also often caused by looking at something like:
String myString = "this";
myString = myString + " more";
System.out.println(myString); //outputs 'this more'.
However here we actually havent mutated myString into the second longer string, we have created a whole new string, copied the original strings contents into it with the ' more' added to the end.
Hence this is actually quite an expensive operation memorywise and computationally although at face value it does not appear to be.
If you want a String to be mutable you must use a StringBuffer, but be aware that this works a bit differently to a String (like there is no '+' operator defined for StringBuffers)
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Deadsea:
Strings are immutable to make String.substring() efficient. If they were not immutable, String.substring() would have to copy data because changing the original string might change the substring. As it is now, it can just point to a place within the string.


Strings are immutable not only for performance reasons (although as Karl pointed out manipulating immutable objects can have poor performance), but for security and thread safety. If you have a few minutes at a bookstore, you might want to thumb through Effective Java by Joshua Bloch. There is some great stuff in there about immutable objects.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic