Originally posted by anil kumar:
Given the following,
13. String x = new String("xyz");
14. y = "abc";
15. x = x + y;
how many String objects have been created?
My Answer is 3,but the source saying 4.
source---->Knowledge Testing Engine
How come ?
4 is the correct answer
String x = new String("xyz"); // String "xyz" placed on literal pool, also String "xyz" created on heap by new operator and assigned to x
14. y = "abc"; String "abc" placed on literal pool and assigned to y
15. x = x + y; String "xyzabc" created on heap and assigned to x