Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

how many String objects created??

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How many String objects will be created when this method is invoked?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 would be created. toString returns a String and that object is already there in the pool.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Five string object are created
They are: Fred,47,fred47,ed4,ED4.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred also created when the class is loaded. so it makes Fred and 47 at class loading time. SO 3 at runtime.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5 object will be created in all. They won't be asking runtime or compilation time.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Nitish

forgot there was also "47" hope,i won't get used to overlooking things
 
Nothing up my sleeve ... and ... presto! A tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic