posted 19 years ago
hum. I think there's some mistake here. Maybe someone else can explain better.
I think there are five and ten String objects for the questions #1 and #2, respectively, created and put in the String pool.
--------- F i r s t Q u e s t i o n ---------------
// Creates the first String object in the pool: "Java"
String x = "Java";
// Creates simultaneously two String objects :
//" Rules!" and "Java Rules!". The last created String object is not
// assigned to any variable but still exists in the pool
x.concat(" Rules!");
// At this point we have three objects in the String pool.
// So, with the last statement we are going to add more two String
// objects in the pool : "x = " and "x = Java"
System.out.println("x = " + x);
Total of String objects in the pool :
----------------------------------------
5 Objects : "Java", " Rules!", "Java Rules!", "x = " and "x = Java"
--------- S e c o n d Q u e s t i o n ---------------
// Creates the first String object in the pool : "spring "
String s1 = "spring ";
// Creates simultaneously two String objects :
// "summer " and "spring summer "
String s2 = s1 + "summer ";
// Creates simultaneously two String objects :
// "fall " and "spring fall "
s1.concat("fall ");
// Creates another one : "spring summer spring "
s2.concat(s1);
// Creates simultaneously two String objects :
// "winter " and "spring winter "
s1 += "winter ";
// At this point we have eight objects in the String pool.
// So, with the last statement we are going to add more two String
// objects in the pool : " " and "spring winter spring summer "
System.out.println(s1 + " " + s2);
Total of String objects in the pool :
----------------------------------------
10 Objects : "spring ", "summer ", "spring summer ", "fall ", "spring fall ", "winter ", "spring winter ", " " and "spring winter spring summer "
I'm really sure about the statements before the println call. I would like to hear your opinions about the println statements. I think they also create String objects in the pool.
[ April 18, 2006: Message edited by: Edisandro Bessa ]
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself