Immutable property of String
How is "once initiated, a String object is immutable" expressed in codes?
I tried the following codes:
publicclass App{
publicstaticvoid main(String[] args){
String x ="abc";
System.out.println(x);
x ="xyz";
System.out.println(x);
}
}
Output:
abc
xyz
Why is the reference variable x able to be reassigned?
Thanks.
The String object is immutable. You can change the reference. That's why this does not work:String s = "abc";System.out.println(s);s.toUpperCase();System.out.println(s);
Thanks. I still do not get the idea of "immutable"
I tried this:
String s = "abc";
System.out.println(s);
System.out.println(s.toUpperCase());
Output:
abc
ABC
so what does this mean? Why is toUpperCase() provided for the String object if it should not work?
Thanks.
Or is there a more obvious example for a beginner?
Thanks.
String s = "abc";System.out.println(s);System.out.println(s.toUpperCase());System.out.println(s);Try this. If String is mutable the output should be:abcABCABCBut the answer is not.
When it says that Strings are immutable it means that a String object once it has been created cannot be changed. I repeat the OBJECT cannot be changed.
String s = "abc";
s = "xyz";
Here, s is a variable not an object. However it references a String object. That object being the letters "abc". On the second line you do not change the "abc" String object, that still exists however the variable s now refers to a second newly created String object being the letters "xyz".
The state (contents) of the String object cannot change after creation. You can cause a String reference variable to point to a different String object, and you can call toUppercase, which returns a new String object, but you can't change which characters are held by a String after it's created.
Thanks flounder, got that part.
Icycool: I tried this:
String str = "abc";
System.out.println(str);
str = str.toUpperCase();
System.out.println(str);
Output:
abc
ABC
so it does show that str is "mutable" (I know that String object is immutable:-))Any other examples?Thanks.
> > String s = "abc";
> System.out.println(s);
> System.out.println(s.toUpperCase());
> System.out.println(s);
>
>
> Try this. If String is mutable the output should be:
> abc
> ABC
> ABC
If string were mutable AND toUpperCase was written to modify the String object in place. String could be mutable and toUppercase still return a new String.
> Thanks flounder, got that part.
>
> Icycool: I tried this:
>
> > String str = "abc";
> System.out.println(str);
>
> str = str.toUpperCase();
> System.out.println(str);
>
> Output:
> abc
> ABC
>
> so it does show that str is "mutable" (I know that
> String object is immutable:-))Any other
> examples?Thanks.
His point was that if String were mutable and toUppercase was written to modify the string in place, then you'd see str's value change *without* doing the str= reassignment.
No you haven't got it.Strings are NOT mutable, they CANNOT change. Calling toUppercase or toLowercase returns a new completely different String. So "abc" still exists unchanged and now there is a new "ABC" string.
Nevermind. I misread.Message was edited by: jverd
> Thanks flounder, got that part.
>
> Icycool: I tried this:
>
> > String str = "abc";
> System.out.println(str);
>
> str = str.toUpperCase();
> System.out.println(str);
>
> Output:
> abc
> ABC
>
> so it does show that str is "mutable"
The variable str being mutable is not the issue. All variables are "mutable" if they're not declared final. However, "mutable" is a term we use for characterizing objects, not variables. Here, all we did is point the reference variable str to a diferent String object.
When we talk about Strings being immutable, we mean the contents of the object cannot change. That's a property of the class, and applies to ALL String objects. If you were to talk about "mutability" of a variable--such as str, above--the only thing that would make sense would be whether it's final, and tha applies to individual variables. It's not a property of any family of variables (except those explicitly declared final).
Message was edited by:
jverd
Any bets on how long it takes the next person to ask this question?
> Any bets on how long it takes the next person to ask> this question?Pointless. My system clock is only accurate to about 10 ms.
Test this program:
public static void main(String args[]) {
String str="1234";
System.out.println("str before is ........"+str);
str.concat("abc");
System.out.println("str after is ........."+str);
StringBuffer s=new StringBuffer("abcd");
System.out.println("String buffer before is ........."+s);
s.append("123");
System.out.println("String buffer after is........."+s);
}
Out Put is:
tr before is ........1234
str after is .........1234
String buffer before is .........abcd
String buffer after is.........abcd123
so what does it mean is
String str="abc";
str.concat("123");
then str cant be abc123 because String Object is IMMUTABLE
but if do String str="abc";
str="123";
then it can be abc123 why because initially str is a reference to a string object called abc so next time u r changing that object content not modifying the object reference i.e str="123" will be a new reference to a string object called 123 ...GOT IT!!!!!!!!!!!!! i think this is a nice approach
> > Any bets on how long it takes the next person to> ask> > this question?> > Pointless. My system clock is only accurate to about> 10 ms.I haven't seen this question for a long time though.
> but if do String str="abc";
> str="123";
> then it can be abc123 why because initially str is a
> reference to a string object called abc so next time
> u r changing that object content not modifying the
> object reference
Incorrect.
That just changes which object the reference points to. It does NOT change the object's content.
jverda at 2007-7-21 18:01:03 >

ya thats what iam saying it never changes object's content it changes an objects reference , see my answer clearly...
ya changing the object content with new reference allocation ...this is correct i think so............
> ya thats what iam saying it never changes object's> content it changes an objects reference ,> see my> answer clearly...Well, your reply #16 seems to talk otherwise!
> ya thats what iam saying it never changes object's
> content it changes an objects reference , see my
> answer clearly...
You said: u r changing that object content not modifying the object reference
That is the opposite of what happens. The reference changes, the object's content does not.
jverda at 2007-7-21 18:01:03 >

what happens is when you try the following: (go thru the commens i am adding on every line)
-
String s = "abc";//new String object "abc" is created
System.out.println(s); //s.toString() is printed which is first output
System.out.println(s.toUpperCase());/* here s.toUpperCase() will return a new Strng
object as String object is "immutable". new
object is having value "ABC". it is a new object
not the original s. try the line after comment
*/
//try the following line, it shows s is still referring to abc
System.out.println(s);
Output:
abc
ABC
//output must be abc here
Thanks everyone.SaiRam, I understood the concept from your codes. Thanks.