• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String - immutable

 
Malli Raman
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • 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.
 
Ivor Horton
Author
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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
 
Steve Deadsea
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • 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.
 
Doug Wang
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the reference of a string object changes not the string object itself.
 
Karl Laird
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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)
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic