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 difference K&B P:420 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 "difference K&B P:420 " Watch "difference K&B P:420 " New topic

difference K&B P:420

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

In K&B P:420 it is given as

String s="abc";

String s=new String("abc");

And its said that these will create 2 objects and 1 reference

My Question is:
Since the two strings points to same content what is the need for creating two objects. Wont the string constant pool checks this.........

Thanks in Advance

Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Pinkal Patel
Ranch Hand

Joined: Jun 16, 2006
Posts: 57
When you are using new String("123");

Will create unother Object without cheaking the Pool.

and when unig s="123";

It will first check pool and create object if it is not found in the Pool

May be this will clear your doubt.

Pinkal Patel<br />SCJP 1.5<br />Preparing for SCWCD
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Pinkal
I agree. Here's the link:
subject: difference K&B P:420
It's not a secret anymore!