This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "creating string objects" Watch "creating string objects" New topic
Author

creating string objects

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I have found this question in whizlab simulator:
(I do not remember the right question but I believe it was something like this)

How many String objects were created?
Please explain me that line by line. Thanks!


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

These type of questions are asked so many times here, just try searching a bit you wil find so many threads. All the String literals (if it was not met before in the code), concatenation,toUpperCase() etc.. will create a new object but toString() will not.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

So why the correct answer is:3 ?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

If you do a search in the forum for "makinStrings", you'll find older threads in which the same question is discussed:

http://www.coderanch.com/t/451570/Programmer-Certification-SCJP/certification/many-String-objects-are-created
http://www.coderanch.com/t/452618/Programmer-Certification-SCJP/certification/String-Object
http://www.coderanch.com/t/426859/Programmer-Certification-SCJP/certification/many-String-Objects-would-created
http://www.coderanch.com/t/426401/Programmer-Certification-SCJP/certification/String-Objects
http://www.coderanch.com/t/268967/Programmer-Certification-SCJP/certification/String-Test

etc...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

OK, thanks - almost everything was discussed here
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

This type of question creates a lot of confusion for people who refer to K&B. It misses the statement that string literals are created when the class is loaded. Well that's how its 3.....seems tricky. and toString doesn't create a new object because that is already there in the pool.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
I'm going to leave a deliberately cryptic response:

If you're not sure of your sources, you might end up spending a lot of time studying topics that you don't need to study.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Sorry...but its just to know stuff that's all
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Bert Bates wrote:I'm going to leave a deliberately cryptic response:

If you're not sure of your sources, you might end up spending a lot of time studying topics that you don't need to study.


Every question is worth studying. Even if it won't appear on SCJP
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8805
    
    5
Of course there's lots of great stuff to study that's not on the exam - but this is the SCJP forum

 
Consider Paul's rocket mass heater.
 
subject: creating string objects
 
Similar Threads
GC Doubt
Difference between String s = "Marcus"; vs String s2 = new String("Marcus");
How many String objects are created
String Objects
creating new Strings(from K&B)