aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String Objects would be created? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many String Objects would be created?" Watch "How many String Objects would be created?" New topic
Author

How many String Objects would be created?

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

How many String Objects would be created by the following code:


Five according to me: "Fred" and "47" literals will be alloted from the String pool. line 13 will result in 2 objects. One each on Line14 & 15.

Enlighten please....


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Hello Monu,

This question has been asked many, many times before on JavaRanch. Please do a search, and search for "Fred 47". You'll find dozens of old discussions about this exact same question.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
This question has appeared so many time.

The answer is 3.

Here are the points










Monu Tripathi wrote:How many String Objects would be created by the following code:


Five according to me: "Fred" and "47" literals will be alloted from the String pool. line 13 will result in 2 objects. One each on Line14 & 15.

Enlighten please....
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Monu you have eaten the main part of the question, that was asking how many objects will be created when this method will be called. See we have remembered this question and you have asked it in wrong way.

You are right, there will be 5 objects if the whole life time of class is considered means from compiling ".java to .class" to loading and running this method.


SCJP 6
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I didnt know this would have been asked already. Nor did i realise that this question is part of some std questionaire. Thanks for replying though.
Vinayak Bhat
Ranch Hand

Joined: May 15, 2008
Posts: 55
I think it should be only one String object that is created because this object is not created on the heap. It is a String literal. Right??? So every time it is modified. No new object is created. the reference is directed to the existing String.


- Vinayak Bhat
SCJP 5 OCEJPAD6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
You are seeming wrong Vinayak.

Please express your understanding with writing code line by line and explaining where you are assuming object created, and when created.
Vinayak Bhat
Ranch Hand

Joined: May 15, 2008
Posts: 55



Note : Page 420: KS && BB When the compiler encounters a String literal, it checks the String constant pool. if match is found, no new String literal object is created, the reference directs to the existing String. Had it been like String s = new String("Fred"); then it would be different.


If my understanding is wrong, then please correct me.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Vinayak Bhat wrote:


Note : Page 420: KS && BB When the compiler encounters a String literal, it checks the String constant pool. if match is found, no new String literal object is created, the reference directs to the existing String. Had it been like String s = new String("Fred"); then it would be different.


If my understanding is wrong, then please correct me.


So what do you think how many Objects will be created, when you call your makinStrings() method, and on which line they will be created?
Vinayak Bhat
Ranch Hand

Joined: May 15, 2008
Posts: 55


OK. Final answer: 5 objects. This is it. If i am wrong, let me know why?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


So I counted 3 Objects when you call makinStrings() method. Am I wrong?
Vinayak Bhat
Ranch Hand

Joined: May 15, 2008
Posts: 55
I am not so much of a pro. I am yet to give my certification. I'll take your word. It is 3. Thanks.

If you could clarify the objects created at compile time and runtime, that would be very much appreciated. Like Why Line 14 and 15, objects are not created at compile time and only created at runtime and so on.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
http://www.coderanch.com/t/268070/Programmer-Certification-SCJP/Strings
http://www.coderanch.com/t/426149/Programmer-Certification-SCJP/String-Objects
http://www.coderanch.com/t/424682/Programmer-Certification-SCJP/String-literal-Pool
http://www.coderanch.com/t/424671/Programmer-Certification-SCJP/What-output-related-to-string
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Here's a puzzle for all you ranchers...

Why might it be a waste of time to study mock questions when you don't know where they came from?


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

Joined: Sep 03, 2007
Posts: 213
Bert Bates wrote:Why might it be a waste of time to study mock questions when you don't know where they came from?

because
Bert Bates wrote:as always - the real exam never uses String objects in GC related questions!
here http://www.coderanch.com/t/268694/Programmer-Certification-SCJP/Garbage-Collection#1247617


So, though it is good to learn, it wont be on the exam............right Bert?

That's the reason, I don't involve in such posts........there's so much more to study.....


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many String Objects would be created?