• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Urgent String question

 
Mohit Goyal
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reference K&B page 360 Ch6: java.lang According to Kathy:
String s=new String("abc"); //will result in two objects - one on the heap and
//the other("abc") in the string pool.
Well my Question is that if after the above statement we have:
String s1="abc";
Then will a new object be created on the pool or the reference of object already in the pool
will be assigned to S1.

Also if we originally had : String s="abc";
String s2=new String("abc");//2
then how many objects will be created on line 2?
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the compiler sees a String literal like "abc" the first time, it gets interned and placed on the String constants pool.
If you afterwards create a new String object like
String str=new String("abc");
only one object is created on the heap. However, should "abc" literal not have been encountered before, it would have been interned first, so you can say that 2 objects were created.
 
Ramil Kilates
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. After line 2, two objects will be created.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic