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

hi am an engineering student and my next plan is for ms in either in us or australia i got my passport but i dont know anything bout visa can u give brief idea 1)How to get prepared for visa and 2)How to apply for top universities and 3)How to pay the fee and so on These all are basic questions plz give me a clear idea

1702


How to create com object in Java?

869


How can we access some class in another class in java?

788


How java is similar to c?

820


Define the term string pool?

811


What is time complexity algorithm?

818


What are the parts of methodology?

774


What is variable and example?

753


How do you create a reference in java?

728


How do you avoid global variables?

804


What is boolean false?

767


How to create a fecelet view?

794


Why local variables are stored in stack?

743


Can we declare a constructor as final?

947


What are different types of expressions?

780