String is an immutable object.
Then how can the following code be justified.

String s1 = ?ABC?;
String s1 = s1+?XYZ?;
s.o.p(s1);

The output is ABCXYZ, which is the value of s1 ?

Answer Posted / k.shiva

here we define s1="ABC".That means one new string is
created.And this string value is stored in heap its
refrence variable is s1.when we say s1+"XYZ".Then string s1
i.e ABC and XYZ are concadinated by using concadination
operater.It forms a new string i.e "ABCXYZ" in heap.But
again we assign this value to the refrence variable
s1.Then "ABCXYZ" value is assign to s1 and the string "ABC"
which is garbage collected.

Is This Answer Correct ?    12 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can a class be a super class and a sub-class at the same time? Give example.

796


How do you sing an Applet ?

2030


What is difference between path and classpath in java?

483


What is object english?

583


State some advantages of java?

712






What is the difference between final, finally and finalize()?

541


What are the steps in the jdbc connection?

572


What is the new line character?

558


What is the difference between the prefix and postfix forms of the ++ operator?

552


Is string an object?

651


What is javac in java?

558


How is string immutable in java?

541


Explain about main thread in java?

610


How listener identify that the event came from a particular object?

1469


What is difference between float and double?

501