I was asked this question:
String s = "abc";
In this simple case, "abc" will go in the pool and s will refer to it.
String s = new String("abc");
Based on the above details, how many String objects and how many reference variables were created before the println statement below the code?
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);
My answer was The result of this piece of code is spring winter spring summer
There are two control variables: s1 and s2. There were eight String objects created as follows: "spring", "summer" (lost), "spring summer", "fall" (lost), "spring" lost, "spring summer spring" (lost), "winter" (lost), "spring winter" (currently "spring" is lost).
In this process, only two of the eight String objects are not lost.
Is it correct?