String object

. Strings are immutable.But String s="Hello"; String s1=s+"World"; S.O.P(s1); means printing "HelloWorld". How ?
[119 byte] By [namita.pradhan08a] at [2007-11-26 16:27:14]
# 1
The string s1 is not the same string as s.
paulcwa at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 2

> . Strings are immutable.But String s="Hello"; String

> s1=s+"World"; S.O.P(s1); means printing "HelloWorld".

> How ?

Well String is immutable as you say, so the only way your example can work is by the creation of new String objects.

s+"World"

involves the creation of a new String object, namely an object consisting of the concatenation of s with "World".

paulcwa at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 3
> S.O.P(s1);Do you rent your keyboard and get charged per character?
paulcwa at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 4
. Strings are immutable.But String s1="Hello"; String s1=s+"World"; S.O.P(s1); means printing "HelloWorld". How ?
namita.pradhan08a at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 5

> . Strings are immutable.But String s="Hello"; String

> s1=s+"World"; S.O.P(s1); means printing "HelloWorld".

> How ?

Here, s1 and s are two different String objects. That is the reason it prints 'HelloWorld'.

Even when you say, s = s + "World"; the initial s is destroyed and a new s is created which will have the value 'HelloWorld'.

Phenoma at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 6

> . Strings are immutable.But String s1="Hello"; String

> s1=s+"World"; S.O.P(s1); means printing "HelloWorld".

> How ?

Well, to understand this you got to know that all objects in Java are represented by references (pointers) to the actual object values.

String s1="Hello";

S1 will hold a reference to an object representing "Hello".

In

s1=s1 + "World";

first a new String object will be created representing "HelloWorld". Then the reference pointing to this object will be assigned to s1.

So String objects are immutable but the reference variables holding pointers to the object are not, unless you declare them final like this,

final String s1="Hello";

Now the String reference variable is immutable too as well as the "Hello" object.

Phenoma at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 7

> Strings are immutable.But> String s1="Hello";

> s1=s1+"World";

> System.out.println(s1);

> means printing "HelloWorld". How ?

Strings are immutable.

String references are mutable (unless declared final).

Your example uses two String literals from the constant pool.

These Strings are added to the String pool.

These Strings are immutable.

You concatenate them creating a new String.

The new String is added to the String pool.

The new String is immutable.

Your String reference "s1" is mutable.

tschodta at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 8
Thanks emble for your reply
namita.pradhan08a at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 9
embla now tell me what will happen to string object "hello''.now where it will be stored .s1 indicates new string object."hello" will be garbage collected or it is still in the memory
namita.pradhan08a at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 10
what is string pool?
namita.pradhan08a at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 11

> embla now tell me what will happen to string object

> "hello''.now where it will be stored .s1 indicates

> new string object."hello" will be garbage collected

> or it is still in the memory

String is not an ordinary class. It has so called "special language support" so it behaves kind of odd compared with other classes.

A String literal like "hello" will be stored in the string literal pool the first time it's used in a program. After that one object will be allocated representing "hello" and stored on the heap. This object will never be GC'ed and it will always represent the "hello" string literal during the program. This is why if you do,

String s1 = "hello";

String s2 = "hello";

S1 and s2 will hold the same object reference namely the one representing the "hello" literal. If you instead do

String s3 = new String("hello");

S3 will differ from s1 and s2. It's because the new operator always creates a new object but this object still will use the "hello" literal stored in the string literal pool.

The object which s3 holds a reference to can be GC'ed but never the object s1 and s2 references. This object stays. It's associated with the "hello" literal during the whole program.

namita.pradhan08a at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...
# 12
Thank you very much embla for your reply.
namita.pradhan08a at 2007-7-8 22:51:25 > top of Java-index,Java Essentials,New To Java...