• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

String literal pool doubt

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:Yes, Constructor should run after that literal loading.


Actually, I would have thought that literal loading would be done at the time the class is loaded, but I'm certainly no expert. I just can't see the point in wasting time knowing all there is to know about something that makes almost no difference to me as a programmer.

To me, about all the pool means is that you almost never need to use new String(String), unless you specifically want a clone of a String (indeed, if the supplied String is a substring, you'll get a "deep" clone).

Winston
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:

saloni jhanwar wrote:The matter was how String objects are being created on pool and heap when these two statements execute in different order that's it.And these two statement will behave totally different while they will execute,and this matter i am telling him.Is anything wrong in it ?


As Campbell has said, those two lines will behave exactly the same. A String object containing the value "abc" will be created in the String pool when the class is loaded. That string will exist even if those two lines of code are never executed.
Therefore the line will simply cause s to reference the already existing String from the pool and the line will create a new String object which will be referenced by s1 and will have the same value as the String in the pool.
Therefore the order of excution is irrelevant


I didn't get because i didn't find it well explained those two lines wont execute even then string literal will exist ? what does it mean ? that literal is exist because of string literal statement in code without that statement you think literal will come automatically.this part was not well explained.object will be created on pool because of that one of statement when string constructor will execute not automatically.
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you had the following code in your class

Now suppose that when you run the program 'condition' is always set to false, so line 4 is never executed.
You might think that only "xyz" would be created in the string pool. But this is not true - both "abc" and "xyz" will be created in the string pool when the class is loaded, so even though line 4 is never executed, "abc" is still created.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Literals - not just String literals - defined in a class' source code are stored by the compiler in binary format in the constant_pool table of the .class file (see paragraph 4.4 of the JVM spec). Note that this is done at compile time.
This per-class constant_pool table is used to populate the runtime constant pool when a class is created (see paragraph 5.1 and paragraph 5.3 of the JVM spec).

In context of your question this means that the String literals you defined will exist in the String runtime constant pool after the class is created, and before any constructors or initializers (static or otherwise) have been invoked.
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:Suppose you had the following code in your class

Now suppose that when you run the program 'condition' is always set to false, so line 4 is never executed.
You might think that only "xyz" would be created in the string pool. But this is not true - both "abc" and "xyz" will be created in the string pool when the class is loaded, so even though line 4 is never executed, "abc" is still created.



Do you mean two objects would be created on pool with "abc" and "xyz" value without execution of these statements at class loading time ? but i am not getting without execution of these statements how string constructor would be execute and objects would be created over there on pool or you just want to say that only literals would be loaded and no objects wont be created at loading time ? and objects would be created on pool at the time of statements execution?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:Do you mean two objects would be created on pool with "abc" and "xyz" value without execution of these statements at class loading time ?


Correct.

saloni jhanwar wrote:but i am not getting without execution of these statements how string constructor would be execute and objects would be created over there on pool


That's part of what the classloader does. See Jelle's post above if you want all the gory details.

saloni jhanwar wrote:or you just want to say that only literals would be loaded and no objects wont be created at loading time ? and objects would be created on pool at the time of statements execution?


A literal is a String object and these objects are created at class loading time.
Or to put it slighly differently - a String literal in the code is an instruction to the classloader to create a String object when the class is loaded.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

saloni jhanwar wrote:Do you mean two objects would be created on pool with "abc" and "xyz" value without execution of these statements at class loading time ? but i am not getting without execution of these statements how string constructor would be execute and objects would be created over there on pool or you just want to say that only literals would be loaded and no objects wont be created at loading time ? and objects would be created on pool at the time of statements execution?


Saloni, this is getting a bit tedious. Jelle's last post provided you with links to the relevant paragraphs in the JVM spec. If you want to find out exactly what and why is going on with the String pool, I suggest you read them.

Winston
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all now i know little bit about literal and string pool.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic