• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How many String Objects

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you pl tell me how many String Objects are created in all the below cases ?

Case1:

String s1 = null;
String s2 = null;

Case2:
String s1="";
String s2="";

Case3:
String s1 = new String("");
String s2 = new String("");

Case 4:

String s1 = "Testing";
String s2 = "Testing";

Case 5:
String s1 = new String("Testing");


My answers :

Case 1:
1
Case2:
1
Case3:
2

Case 4:
1

Case 5:
2

Is this correct ?

[ July 05, 2006: Message edited by: susan waters ]
[ July 05, 2006: Message edited by: susan waters ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion:

Case 1:
0 - because null is not object
Case2:
1 - only empty string ""
Case3:
3 - empty string, s1 from empty string, s2 from empty string
Case 4:
1 - only "Testing"

All string literals creates only once and locating in string pool
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Case 5:
String s1 = new String("Testing");
2 objects
 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many String obejcts are created in this case ?

String s1 = new String("English");
String s2 = new String("English");

I know when we use new it will force to create new object. So it will create two obejcts. But my question is how many obejcts created for pool ?

When I use only one statement it creates two obejcts. When I use both how many obejct created in total ?

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


Originally posted by Susan Waters:
How many String obejcts are created in this case ?

String s1 = new String("English");
String s2 = new String("English");

I know when we use new it will force to create new object. So it will create two obejcts. But my question is how many obejcts created for pool ?

When I use only one statement it creates two obejcts. When I use both how many obejct created in total ?



When compiler encounters a String literal, it first searches same literal in pool, if it finds the same literal object in pool, it won't create a new literal object in pool.

String s1 = new String("English");
String s2 = new String("English");

In this case,

One literal object "English" will be created and placed in pool by the compiler.

At runtime, two object will be created by new on heap.

Regards

Naseem
[ July 05, 2006: Message edited by: Naseem Khan ]
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So,

String s1 = new String("English");
String s2 = new String("English");

creates 3 String objects, one is placed in the String pool and the rest two in the heap. Am I right?

I guess the objects placed in the heap indirectly refer to the object placed in the String pool.

regards,
vijay.
 
susan waters
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Yes that's what my guess.
By the time S1 executes it creates two obejcts. One obejct is in the pool. in the second stmt since we are using "new" it should forcefully creates another object but it never creates another one to put in to the pool. Correct ???
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah you are right...

Placing the String literal objects in pool is done by compiler not by runtime system.
 
That new kid is a freak. Show him this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic