Re: Strings...immutable?
printdude1968@gmail.com wrote:
On Mar 18, 10:51 pm, Patricia Shanahan <p...@acm.org> wrote:
John T wrote:
...
After doing a bit more studying, I've learned that it's the contents of
the string object that are subject to change, not the string itself,
hence the idea/rule that strings are immutable. Is this a correct
interpretation or do I need to go back to the books again?
No, the contents of a String object can never change.
Returning to a program I posted earlier in this thread:
1 public class Concatenate {
2 public static void main(String[] args) {
3 String s = "hello";
4 String x = s;
5 s += "good-bye";
6 System.out.println(x);
7 }
8 }
A reference variable such as s or x is either null, or a pointer to some
object.
At line 3, s is assigned a pointer to the object representing the String
literal "hello".
Pointers in Java? Ok.. I follow so far
I think a lot of confusion has been caused by the developers of Java, at
least in the early stages, avoiding talking much about pointers. Java
reference variables are pointers in the sense in which the term was used
e.g. in the Pascal Report.
Unfortunately, C added a lot of baggage such as arithmetic and
conversions to/from integer types. There may have been a concern that if
Java references were called pointers people would think they were as
dangerous as C pointers.
A lot of Java behavior, including this topic and some aspects of
parameter passing, are much easier to understand in terms of pointers.
At line 4, that pointer is copied to x. They now both point to the same
object.
Yup.. gotcha
Line 5 is equivalent to 's = s + "good-bye";'. The JVM creates a String
object representing the concatenation of the String object s references
and the one representing "good-bye". s is assigned a pointer to that
object.
I understand the concept of concatenation. Is 's' now a new String or
is
the old one reused?
s is now a pointer to a new String object, containing the contents of
the one previously referenced by s followed by "good-bye". It has to be
a different one because the old one still contains "hello", as did when
created and will go on doing until it is GC'd or the program terminates.
I'm not sure we are using "concatenation" exactly the same way. For
purposes of Java, concatenating two String objects results in a
reference to a String object containing the concatenation of their
contents. It does not mean changing one of them by inserting the
characters from the other.
The output at line 6 shows the value of the object x references, the
original, unmodified "hello".
But that doesn't make sense. If x and s are both pointing to the same
chunk of memory in which
is contained a string "hello", my reading has lead me to believe that
if you change s, x is
changed as well. so if you were to say s += "hello"; and then output
x, you would get the
same thing as if you output s.
x and s being pointers to the same object is just like intX and intX
being ints with the same value in the following code:
int intS = 3;
int intX = intS;
intS = 7;
You would expect intX to go on being 3, even though intS now contains 7.
In just the same way, x goes on referencing the "hello" object
regardless of any assignments to s.
So where the hell did I get the idea that x would change along with
s? Surely I didn't come up with it on my own?
Unfortunately, I don't know. If I knew where the idea came from, I could
probably write exactly the right explanation....
Patricia