File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many String Objects would be created?

 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prav sharma
Ranch Hand
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


OK. Final answer: 5 objects. This is it. If i am wrong, let me know why?
 
Punit Singh
Ranch Hand
Posts: 952
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So I counted 3 Objects when you call makinStrings() method. Am I wrong?
 
Vinayak Bhat
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sachin Adat
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic