How many String objects will be created with the given Java code?

 

String x = new String("yz");
String y = "abc";
String t = "efg";
x = x + y + t;


The answer is that there will be 5 String objects. Line 1 creates two, one referred to by x and the lost String "yz". Line 2 creates one (for a total of three). Line 3 creates one more(for a total of four). Line 4 creates one more (for a total of five), the concatenated String referred to by x with a value of "yzabcefg".

 


Related #tags:     #java     #core-java    

Related questions




Looking for change? Click below to prepare for technical interview.


Technical Interview Questions