Can anybody explain me how there are 4 objects created? I know 3.
3. Given the following, 13. String x = new String("xyz"); 14. y = "abc"; 15. x = x + y; how many String objects have been created? A. 2 B. 3 C. 4 D. 5
Ans--C. Line 13 creates two, one referred to by x and the lost String “xyz”. Line 14 creates one (for a total of three). Line 15 creates one more (for a total of four), the concatenated String referred to by x with a value of “xyzabc”. A, B, and D are incorrect based on the logic described above.
Perhaps its the creating of *two* strings on line 13? One is the literal "xyz" (created in a similar way to "abc" on the next line) and the other is created when the "new" operator is invoked. x refers to the "new" String; as stated the "xyz" literal is "lost".