String s = "";
does not create objects.
s + = new line ("a");
creates five objects. new String
, StringBuilder and its char [] and the resulting string and char []
S + = "b";
creates four objects: StringBuilder and its char [], and string and char []
So, I get a total of nine objects, of which three String objects
Note. You can be sure that "" is already loaded, as it appears in many system classes, including ClassLoader and Class.
Lines "a" and "b" may or may not be considered new lines for the purpose of this question. IMHO I would not count them, because they will be created no more than once, and if this code is run only once, it is hardly important how many lines are created. It is most likely useful to know how many objects are created each time the code is executed.
source share