Java strings are immutable.  The thing to keep in mind is that a variable
contains a reference to a string, not the string itself.  Changing the
variable changes the reference, i.e. causes the variable to refer to a
different string.  For example,

String s1 = "abc";
String s2 = s1;
// Here s1 and s2 both refer to the same string
s2 = "def"
// The value refered to by s2 has been changed.  s1 is, of course, not
effected.


On Fri, Jan 29, 2010 at 11:18 AM, Paul Wilkin <[email protected]>wrote:

>  >
>
> > I've seen written in several places that JAVA Strings are
>
> > "Immutable" eg. Constant, unchanging. And that if you declare
>
> > several identical String Variables, without using NEW, Java points
>
> > all the variables at the same object.
>
> >
>
> > One would then suppose:
>
> >     1) If three variables are all references to a single object,
>
> > changing the value of one of them would change all of them.
>
> >     2) Of course, Strings are Immutable, so attempting to change it's
>
> > value should throw a compile-time error.
>
> >
>
> >
>
>
>
> Hi;
>
>
>
> To answer your points:
>
>
>
> "If three variables are all references to a single object, changing the
> value of one of them would change all of them."
>
>
>
> *Strings don't behave like other objects. *
>
>
>
> Let’s say we declare 2 strings like so:
>
>
>
> String s = “abcdef”;
>
> String s2 = s;
>
>
>
> So far, we won’t encounter any difference between String and other object
> behaviour. We’ve created 1 String object and 2 references to the same
> object.
>
>
>
> BUT (here’s where a String acts differently!) if we were to say:
>
>
>
> s = s.concat(“append this”);
>
>
>
> You MIGHT expect s and s2 to still be pointing to the same String object.
>
>
>
> BUT if you say System.out.print(s) you get ‘abcdefappend this’ and if you
> say System.out.print(s2) you get ‘abcdef’.
>
>
>
> The code s = s.concat(“append this”) caused the JVM to create a NEW string
> object and give it the value “abcdefappend this”.
>
>
>
> Then the reference variable s was switched to refer to this new object,
> while s2 still refers to the original string.
>
>
>
> So we end up with 2 Strings and 2 reference variables.
>
>
>
> Thus the String is immutable, and its value cannot change under any
> circumstances but the reference variables are not immutable.
>
>
>
> BTW, if you just say:
>
>
>
> String name = “Fred”;
>
> name = “Barney”;
>
>
>
> Then again you’ll end up with 2 separate strings only now you don’t have a
> reference to the original “Fred”. It’s content still hasn’t changed (it’s
> immutable right!) but its lost and will now be a candidate for garbage
> collection!!
>
>
>
> “Of course, Strings are Immutable, so attempting to change it's value
> should throw a compile-time error.”
>
>
>
> There will not be any compile time errors. You can write code to change a
> String, but you won’t EVER really be changing its value. You’ll be creating
> new Strings and changing the contents of the relevant references.
>
>
>
> Paul Wilkin
>
>
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]<javaprogrammingwithpassion%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to