This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Urgent String question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Urgent String question" Watch "Urgent String question" New topic
Author

Urgent String question

Mohit Goyal
Ranch Hand

Joined: Nov 09, 2003
Posts: 65
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

Joined: Aug 25, 2003
Posts: 504
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

Joined: Nov 27, 2003
Posts: 1
Hi. After line 2, two objects will be created.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Urgent String question