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

What is %d in printf?

532


What does exclamation mean in java?

575


What are the disadvantages of using inner classes?

556


Does sprintf add a null terminator?

564


What are "class access modifiers" in Java?

613






What is rule of accessibility in java?

545


what is recursion in java

599


Explain exception chaining in java?

611


How do you add an element to a hashset in java?

492


What's the default access specifier for variables and methods of a class?

516


Mention some features of java?

541


How do you start a new line in java?

523


What is parsing and its types?

588


What is the maximum size of a string in java?

551


How do you reverse sort a list in java?

512