Nisal Panagoda wrote:This is related 2 scjp. please someone help me.......
String s1="spring";
String s2=s1+"summer";
s1.concat("winter");
s1=s1.concat("autumn");
System.out.println(s1+" "+s2);
Please explain me how many objects are created in the memory and what are thay.
Please consider the bold line too
String s2=s1+"summer";
will be converted to
String s2 = (new StringBuilder(String.valueOf(s1))).append("summer").toString();
System.out.println(s1+" "+s2);
will be converted to
System.out.println((new StringBuilder(String.valueOf(s1))).append(" ").append(s2).toString());
And all other explanations are given in other posts and K&B book.
And it depends on you, whether you want to count only String objects or StringBuilder objects also.