GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes String - immutable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String - immutable" Watch "String - immutable" New topic
Author

String - immutable

Malli Raman
Ranch Hand

Joined: Nov 07, 2001
Posts: 312
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

Joined: Mar 22, 2002
Posts: 67
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


Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
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

Joined: Oct 05, 2001
Posts: 445
It is the reference of a string object changes not the string object itself.


Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep
Karl Laird
Ranch Hand

Joined: Jul 14, 2001
Posts: 67
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)


The Eagle sneers at the Peacock<p>Systems Administrator<br />OrderWare Solutions Ltd<br /><a href="http://www.orderware.net" target="_blank" rel="nofollow">http://www.orderware.net</a>
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String - immutable