This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Creating Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Creating Strings" Watch "Creating Strings" New topic

Creating Strings

kirba devi
Ranch Hand

Joined: Jun 29, 2007
Posts: 50
Hi ranchers
Preparing for scjp 5.while reading scjp5 by kathy siera from page 420.
"Creating New Strings".There she explained two approaches for creating Strings.

1st approach:String s=new String("String");
2nd approach:String s="String".

Told there is subtle difference between those two.Once is created with new and the other is not.Also for the 1st approach she said like two String objects are created because of new?how come?.while latter only 1 object is created with 2nd approach

Deepak Bala

Joined: Feb 24, 2006
Posts: 6661

One String would be in the String pool and the other string would be the one created with the new operator when you type String s = new String("");.

With String s = "hi"; you create one object. This is the one in the String pool

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Don't get me started about those stupid light bulbs.
subject: Creating Strings
Similar Threads
How we can find that how much string objects has been created?
How many objects????
what's intern() in strings
How to get a transfer function to work both ways?
what is the diff? (Strings again)