This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many String objects created?? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "how many String objects created??" Watch "how many String objects created??" New topic
Author

how many String objects created??

vineet walia
Ranch Hand

Joined: Feb 16, 2008
Posts: 138


How many String objects will be created when this method is invoked?
krishna kanthgaru
Greenhorn

Joined: Feb 12, 2007
Posts: 17
1. Fred
2. Fred47
3. ed47
4. ED47

there must something , if a ranch hand is asking. enlighten us , vineet
vineet walia wrote:

How many String objects will be created when this method is invoked?


no signature ... i am not a celebrity yet
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 536
Actually, if you are reading K&B SCJP6 then it can be answered at Pg434. When a string literal is encountered, the string object is created when the class is loaded. SO Fred and 47 are compile time constants and are created when the class is loaded. But at run time Fred47,ed47 and ED47 are created. s.toString doesn't create a new string as that string is already there in the string pool. Read about Compile time Constants


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
3. ed47

ed4 you mean

Total strings ever created during the codes lifecycle is 5. "47" is a compile time constant so does not reach run-time, where only 4 would be created.

Regards


Experience and talent are independent of age
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 536
3 would be created. toString returns a String and that object is already there in the pool.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Nitish Bangera wrote:3 would be created. toString returns a String and that object is already there in the pool.


the others are created before that itself, as told by Krishna
bhavya passion
Greenhorn

Joined: Aug 21, 2009
Posts: 1
Five string object are created
They are: Fred,47,fred47,ed4,ED4.


cheers,
Bhavya
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
bhavya passion wrote:Five string object are created
They are: Fred,47,fred47,ed4,ED4.


Yes Bhavya! But let's re-iterate Nitish's important point that "47" is resolved at compile-time. So 4 left at run-time
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 536
Fred also created when the class is loaded. so it makes Fred and 47 at class loading time. SO 3 at runtime.
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9189
    
    2

This question has been asked many times before. Please search the forum before asking a question

http://www.coderanch.com/t/434127/Programmer-Certification-SCJP/certification/many-String-Objects-are-created

http://www.coderanch.com/t/426859/Programmer-Certification-SCJP/certification/many-String-Objects-would-created

http://www.coderanch.com/t/426115/Programmer-Certification-SCJP/certification/StringObjects

http://www.coderanch.com/t/424671/Programmer-Certification-SCJP/certification/What-output-related-string

..............


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Tanmoy Dhara
Ranch Hand

Joined: Aug 06, 2009
Posts: 62
String s = "fred" will be created when the class will be loaded and the reference will be added to the string pool so that if in future any satement uses "fred",it will just refer to the existing object in stead of making a new one ( although thats not needed here)

so, 3 objects at runtime will be created.

But, i have one question, what if they do not specify runtime or compilation time???Should i write 4 or i assume they are refering to the runtime scenario??


SCJP 5
"I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times" - Bruce Lee
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 536
5 object will be created in all. They won't be asking runtime or compilation time.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
"fred" athough resolved at compile-time, has an active reference at runtime. A class compiled today might be run after N years. If the literal is saved today it cannot stay till N years. As soon as the scope of compilation ends, literal's life ends. The reference needs to be resolved at runtime as well. So "fred" is spawned at runtime also. Then again it depends on the string-pool.

Tanmoy Dhara
Ranch Hand

Joined: Aug 06, 2009
Posts: 62
@ Nitish

forgot there was also "47" hope,i won't get used to overlooking things
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many String objects created??
 
Similar Threads
String Objects...
Strings
How many String objects are created?
How many objects????
creating new Strings(from K&B)