• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

String Pool and Literals

 
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author & internet detective
Posts: 40746
827
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
blossom belle
Ranch Hand
Posts: 145
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic