Home arrow static arrow Java Programming [Archive] - Regarding String
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Regarding String
This topic has 11 replies on 1 page.

Posts:170
Registered: 8/6/04
Regarding String  
Aug 6, 2004 5:42 AM



 
Hello,
I want to ask one thing.
How many objects are created of the String?.

String a = "xyz";
String b = "xyz";
String c =b;
 

Posts:319
Registered: 11/8/00
Re: Regarding String  
Aug 6, 2004 5:44 AM (reply 1 of 11)



 
one i believe.

don't take my word for that
 

Posts:2,909
Registered: 13.8.2003
Re: Regarding String  
Aug 6, 2004 5:44 AM (reply 2 of 11)



 
One
 

Posts:4,496
Registered: 19/06/02
Re: Regarding String  
Aug 6, 2004 6:07 AM (reply 3 of 11)



 
Actually, were you to run that in a real program the execution of those statements would not create any Objects.
 

Posts:196
Registered: 6/29/04
Re: Regarding String  
Aug 6, 2004 6:12 AM (reply 4 of 11)



 
Eka
 

Posts:1,525
Registered: 3/24/04
Re: Regarding String  
Aug 6, 2004 6:15 AM (reply 5 of 11)



 
Un
 

Posts:342
Registered: 5/22/98
Re: Regarding String  
Aug 6, 2004 6:17 AM (reply 6 of 11)



 
Actually, were you to run that in a real program the
execution of those statements would not create any
Objects.

Alright, I'm either missing something incredibly obvious here, or there is something I don't understand. Would you mind elaborating a little or pointing me at a doc or link that would explain that one?

Thanks,

Graeme
 

Posts:10,972
Registered: 10/23/03
Re: Regarding String  
Aug 6, 2004 6:17 AM (reply 7 of 11)



 
Egy
 

Posts:4,496
Registered: 19/06/02
Re: Regarding String  
Aug 6, 2004 6:22 AM (reply 8 of 11)



 
Actually, were you to run that in a real program the
execution of those statements would not create any
Objects.

Alright, I'm either missing something incredibly
obvious here, or there is something I don't
understand. Would you mind elaborating a little or
pointing me at a doc or link that would explain that
one?

Thanks,

Graeme

The reason that the execution of those statements doesn't actually create any Objects is that the runtime constant pool is constructed when the class is created. So by the time you execute those byte codes the Object has already been created and exists in the constant pool.
 

Posts:342
Registered: 5/22/98
Re: Regarding String  
Aug 6, 2004 6:28 AM (reply 9 of 11)



 
The reason that the execution of those statements
doesn't actually create any Objects is that the
runtime constant pool is constructed when the class is
created. So by the time you execute those byte codes
the Object has already been created and exists in the
constant pool.

Phew, I thought you were saying that no Objects would be created at all. I was beginnning to worry that there was something fundamental about Strings or initialization I didn't understand. Guess I should have examined the wording of your original post more closely :)

Graeme
 

Posts:4,496
Registered: 19/06/02
Re: Regarding String  
Aug 6, 2004 6:42 AM (reply 10 of 11)



 
Guess I should have examined the wording of your original post more
closely :)

It's a bit of a "trick" answer, but if anyone ever asks you "how many Strings get created by these statements?" in a job interview, you can say "none" and then amaze them with your knowledge of java memory management. ; )
 

Posts:2,909
Registered: 13.8.2003
Re: Regarding String  
Aug 6, 2004 6:48 AM (reply 11 of 11)



 
in a job interview, you can say "none" and then amaze them with your knowledge of java memory
management. ; )

I'd rather say "with interview questions like that, I don't think I want to work here" and then look at their amazed faces :)
 
This topic has 11 replies on 1 page.