This week's giveaway is in the Spring forum.
We're giving away four 3 month subscriptions to Marco Behler Videos and have Marco Behler on-line!
See this thread for details.
Win a 3 month subscription to Marco Behler Videos this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String Pool and Literals  RSS feed

 
shambhavi sham
Ranch Hand
Posts: 136
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



1.When the class loads, all the literals in the program are in put in the pool. so is it correct that when the class loads and all the literals are being placed in the pool,  "xyz" will have a pool reference but will not yet be assigned to instance variable d ! Only on lines 8 and 9 , when instances are created, will the respective d instance variables, for s1 and s2 respectively, will be assigned their references from the pool . Am I right on this?

2.But if the String d was a local variable inside the main() method, and not an instance variable , will d will be assigned the reference from the String pool when the class loads , or when its statement actually runs/executes ?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37051
507
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While you don't need to know this for the exam, it is interesting. The way to find out what happens is to look at the bytecode. I like using this Eclipse plugin for looking at bytecode.

A general note: in Java classnames begin with an uppercase letter by convention. You might want to rename your file to Scrap so you get used to seeing it right.

shambhavi sham wrote:
1.When the class loads, all the literals in the program are in put in the pool. so is it correct that when the class loads and all the literals are being placed in the pool,  "xyz" will have a pool reference but will not yet be assigned to instance variable d ! Only on lines 8 and 9 , when instances are created, will the respective d instance variables, for s1 and s2 respectively, will be assigned their references from the pool . Am I right on this?

Correct. The bytecode snippet below shows it is set in the constructor:



shambhavi sham wrote:


shambhavi sham wrote:2.But if the String d was a local variable inside the main() method, and not an instance variable , will d will be assigned the reference from the String pool when the class loads , or when its statement actually runs/executes ?

Still when the method executes:



 
shambhavi sham
Ranch Hand
Posts: 136
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By your second byte code snippet, you mean, it 'll be done when that line in main executes ? but the literals alone are put on memory with a pool reference right , when the class loads ?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!