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.

[730 byte] By [missing_linka] at [2007-11-26 20:41:39]
# 1
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);
CaptainMorgan08a at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 2

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.

missing_linka at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 3
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.
Icycoola at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 4

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".

floundera at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 5

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.

jverda at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 6

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.

missing_linka at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 7

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

jverda at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 8

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

jverda at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 9
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.
floundera at 2007-7-10 2:00:20 > top of Java-index,Java Essentials,New To Java...
# 10
Nevermind. I misread.Message was edited by: jverd
jverda at 2007-7-10 2:00:21 > top of Java-index,Java Essentials,New To Java...
# 11

> 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

jverda at 2007-7-10 2:00:21 > top of Java-index,Java Essentials,New To Java...
# 12
Any bets on how long it takes the next person to ask this question?
floundera at 2007-7-10 2:00:21 > top of Java-index,Java Essentials,New To Java...
# 13
> 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.
jverda at 2007-7-10 2:00:21 > top of Java-index,Java Essentials,New To Java...
# 14

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

N.V.SaiRama at 2007-7-10 2:00:21 > top of Java-index,Java Essentials,New To Java...
# 15

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

N.V.SaiRama at 2007-7-21 18:01:03 > top of Java-index,Java Essentials,New To Java...
# 16
> > 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.
qUesT_foR_knOwLeDgea at 2007-7-21 18:01:03 > top of Java-index,Java Essentials,New To Java...
# 17

> 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 > top of Java-index,Java Essentials,New To Java...
# 18
ya thats what iam saying it never changes object's content it changes an objects reference , see my answer clearly...
N.V.SaiRama at 2007-7-21 18:01:03 > top of Java-index,Java Essentials,New To Java...
# 19
ya changing the object content with new reference allocation ...this is correct i think so............
N.V.SaiRama at 2007-7-21 18:01:03 > top of Java-index,Java Essentials,New To Java...
# 20
> 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!
qUesT_foR_knOwLeDgea at 2007-7-21 18:01:03 > top of Java-index,Java Essentials,New To Java...
# 21

> 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 > top of Java-index,Java Essentials,New To Java...
# 22

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

tapan_javaprogrammera at 2007-7-21 18:01:03 > top of Java-index,Java Essentials,New To Java...
# 23
Thanks everyone.SaiRam, I understood the concept from your codes. Thanks.
missing_linka at 2007-7-21 18:01:03 > top of Java-index,Java Essentials,New To Java...