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 difference between iterator access and index access?

853


Which is bigger float or double java?

747


Is an integer an object?

724


How to sort an array in java without using sort method?

723


What is a instance variable?

781


why doesn't java run on all platforms?

783


Why java is called not pure object oriented language?

820


What Is Query Throttling in java?

787


Why does java not support operator overloading?

906


What defines function?

748


What is a function in java?

777


What do bitwise operators do?

743


Explain the selection sort algorithm and state its time complexity?

824


What is java string pool?

771


Can we write any code after throw statement?

824