File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes String / string =>mutable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String / string =>mutable?" Watch "String / string =>mutable?" New topic
Author

String / string =>mutable?

Willie Toma
Ranch Hand

Joined: May 11, 2001
Posts: 78
The folloing statement is true: "The String class represents an immutable string."
Is it true that the string class represents a 'mutable' string?
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
All Strings are immutable. Once created they can NOT be modified in any way.
This is to allow for some cute little "optimizations" in the JVM. Specifically the re-use of defined Strings. If you define the String "String yes = "Y"; in lots of places, the JVM only makes ONE object for it and points all the various uses at the same object. This is fine if you know that it can never change.
However, if multiple uses are all pointing at the same String value - it would be catastrophic if one of the uses tampered with the thing.
Instead there is a StringBuffer class for cases where you know that the object is going to change values.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Willie Toma
Ranch Hand

Joined: May 11, 2001
Posts: 78
If you cannotate two strings and store the result in a new object, does the new object point to the old locations? Or, store the result of f and k?
String f = "Bellis";
String k = "simo!";
String r = f + k;
System.out.println(r); Bellissimo!
Forget about it!
Thanks Cindy, your the greatest.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
A third object is made that r points to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String / string =>mutable?