How many String objects will be created when this code is executed?
String s1 = "abc"; // 1
String s2 = new String("xyz"); // 2
s2 = s1; // 3
s1.toUpperCase(); // 4
String s3 = "abc"; // 5
String s4 = s3.replace('a','A'); // 6
The given answer is 4. I think there are more than 4 as per following:
At line:
1. One in
String constant pool "abc"
2. One on the heap and One in the String constant pool.
3. none
4. One on the heap "ABC" and one in the String constant pool "ABC".
5. none
6. On on the heap "Abc" and one in the String constant pool "Abc".
I think there are 7 objects created. Any comments?
[ October 23, 2003: Message edited by: Barkat Mardhani ]
[ October 23, 2003: Message edited by: Barkat Mardhani ]