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.
How many String objects will be created when this method is invoked?
krishna kanthgaru
Greenhorn
Joined: Feb 12, 2007
Posts: 17
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
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
posted
0
Five string object are created They are: Fred,47,fred47,ed4,ED4.
cheers,
Bhavya
Himanshu Kansal
Ranch Hand
Joined: Jul 05, 2009
Posts: 257
posted
0
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
posted
0
Fred also created when the class is loaded. so it makes Fred and 47 at class loading time. SO 3 at runtime.
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
posted
0
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
posted
0
"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
posted
0
@ Nitish
forgot there was also "47" hope,i won't get used to overlooking things