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
posted
0
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
posted
0
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.