This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String object" Watch "String object" New topic

String object

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
Is it true that:

1. String s1 = "abc";
2. String s2 = s1;
3. String s3 = "abc";

would create 1 object, and

4. String s1 = new String("abc");
5. String s2 = s1;
6. String s3 = new String("abc");

would create 3 object.

Please comment on this.

shetal bansal
Ranch Hand

Joined: May 09, 2005
Posts: 63
R u sure on the no. of objects created in second case as I think the second case will produce 2 objects.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071

Assuming this next section is done in complete isolation of the previous section.

If the two sections of code are loaded by the same ClassLoader then the "abc" String literal will only be loaded once.
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Same type of questions are asked in this forum many times. Please do a search to get more idea.
[ June 23, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
No! Not again!! Please spare us!
This forum is clogged with that question, how could you miss it?

Tony Morris
Java Q&A (FAQ, Trivia)
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Check this article from the forum bartender Corey. This might help you.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

This recent thread on this topic went to 37 posts!

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: String object
It's not a secret anymore!