Is Strings immutable?

1. Plz anyone tell me "Is Strings immutable"?

for ex:String str1 = "india"; // (1)

str1 = str1 + "is gr8";//(2)

S.O.P(str1);// prints "india is gr8" (3)

If strings r immutable, how can its value b changed, if u says that at (2) string object ref str1 is referring to different object, then how to retrive original string object value "india" after concatenation,which is at (1).

[410 byte] By [Mylovegr8a] at [2007-10-3 4:01:49]
# 1

*sigh*

Google.

The string objects don't change. New String objects are created and the variable str1 will reference the newly created String with a different content.

Edit:

As to the second part: If you don't hold a reference to the original String object then it's lost. There's no way for you to get back to it.

JoachimSauera at 2007-7-14 22:01:07 > top of Java-index,Java Essentials,Java Programming...
# 2

> 1. Plz anyone tell me "Is Strings immutable"?

> for ex:String str1 = "india";

>// (1)

> r1 + "is gr8";//(2)

> S.O.P(str1);// prints "india is

> gr8" (3)

>

> If strings r immutable, how can its value

> b changed,

It's not. The value of the reference is changed so that it points to a different String object. This is always what happens when you assign to a reference variable (a non-primitive) in Java.

jverda at 2007-7-14 22:01:07 > top of Java-index,Java Essentials,Java Programming...
# 3

> If strings r immutable, how can its value

> b changed, if u says that at (2) string object ref

> str1 is referring to different object, then how to

> retrive original string object value "india" after

> concatenation,which is at (1).

String str1 = "india";

String str2 = str1 + "is gr8";

You now still have a reference to the original string.

warnerjaa at 2007-7-14 22:01:07 > top of Java-index,Java Essentials,Java Programming...