File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Urgent String question" Watch "Urgent String question" New topic

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

Joined: Nov 27, 2003
Posts: 1
Hi. After line 2, two objects will be created.
I agree. Here's the link:
subject: Urgent String question
jQuery in Action, 3rd edition