This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Creating String Objects 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 "Creating String Objects" Watch "Creating String Objects" New topic

Creating String Objects

Sumi Festus

Joined: Feb 04, 2005
Posts: 14
this was a question i stumbled upon
String x=new String("xyz");
String y="abc";
String x=x+y;

The answer says that 4 String objects are created can someone give me an explanation as to how 2 string objects are being created in the first line...
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
A String instance is created that represents the String literal, which is passed to the String(String) constructor to create yet another String instance.

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: Creating String Objects
jQuery in Action, 3rd edition