• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question from Whizlab

 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think that 4 & 6 are putting anything in the String contant pool?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic