Doubt from some book - How many String objects
Posted: Sat Aug 09, 2014 3:45 am
Hi,
I got little confused after this question. If i apply the knowledge gain from this question, then there must be 9 String object created but the author mentioned 8
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println((s1 + " " + s2));
Here are the instance which i thought:
spring
summer
spring summer
fall
spring fall
spring summer spring
winter
spring winter
spring winter spring summer // the author is not taking this instance into account
Could you please explain. Thanks a lot
I got little confused after this question. If i apply the knowledge gain from this question, then there must be 9 String object created but the author mentioned 8
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println((s1 + " " + s2));
Here are the instance which i thought:
spring
summer
spring summer
fall
spring fall
spring summer spring
winter
spring winter
spring winter spring summer // the author is not taking this instance into account
Could you please explain. Thanks a lot