This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question from Whizlab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question from Whizlab" Watch "A question from Whizlab" New topic
Author

A question from Whizlab

Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787

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 ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Why do you think that 4 & 6 are putting anything in the String contant pool?


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I understand that:
1. any manipulation on a String object will create a NEW object on the heap. This includes toUperCase() method.
2. Whenever a NEW String object is created on the heap, same object is created in the string constant pool if it is not alread there.
Am I mistaken?
Barkat
[ October 23, 2003: Message edited by: Barkat Mardhani ]
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

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

I think 5 object are created
1. 1 (placed string constant pool)
2. 2 (creates new object on heap, and placed xyz in constant pool)
3. none (creating alias)
4. 1 (object with no reference)
5. not a new one (pointing to constant pool)
6. 1
Thanks.
[ October 23, 2003: Message edited by: Cathy Song ]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
2. Whenever a NEW String object is created on the heap, same object is created in the string constant pool if it is not alread there.
#2 is incorrect. String objects are not automatically added to the String constant pool.
I think 5 is the correct answer.
[ October 23, 2003: Message edited by: Thomas Paul ]
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270

To follow up on Thomas's post,
If I change line 2, such that it creates a string object with "abc", now how many objects are created?
I recommend using javap to to view the bytecode.
Thanks.
[ October 23, 2003: Message edited by: Cathy Song ]
 
wood burning stoves
 
subject: A question from Whizlab
 
Similar Threads
Garbage Collection and the String Pool
String
Garbage Collection and the String Pool
== operator returns true for Strings
String puzzle