This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes String literal pool doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String literal pool doubt" Watch "String literal pool doubt" New topic
Author

String literal pool doubt

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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.


Tell the difficulties that i am difficult.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
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.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1761
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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

Joined: May 30, 2012
Posts: 679
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

Joined: Mar 17, 2011
Posts: 7677
    
  19

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

Joined: Feb 09, 2012
Posts: 583

thank you all now i know little bit about literal and string pool.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literal pool doubt