aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String literal Pool doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String literal Pool doubt" Watch "String literal Pool doubt" New topic
Author

String literal Pool doubt

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I have doubt on
1) When we create String s="abc", then where the "abc" literal will be created? In the heap or In the String literal pool?
2) What is stored in String literal pool? String literal's reference or String literal itself?

String Literal Pool tutorial says, String literals are created on heap and their reference are stored in pool.

K&B 6 chapter 6 page 434says :
String s = "abc"; // creates one String object and one
// reference variable
In this simple case, "abc" will go in the pool and s will refer to it.

String s = new String("abc"); // creates two objects,
// and one reference variable
In this case, because we used the new keyword, Java will create a new String object in normal (nonpool) memory, and s will refer to it. In addition, the literal "abc" will be placed in the pool.



String s = new String("abc"); here k&b says two objects will be created one in normal memory and another literal "abc" in the pool.

While according to above tutorial only one object should be created in heap, and its reference will be stored only in the pool.
Tutorial says :




for





Can anybody clear the doubt how many objects should be created when String s = new String("abc"); is called ? and when we do String s="abc"; then "abc" is created on heap or constant pool?


SCJP 6
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Punit Singh wrote:

Can anybody clear the doubt how many objects should be created when String s = new String("abc"); is called ?


I think, its creates one object and one constant in SCP, when execute first time and later on only one object for "abc" !!

Punit Singh wrote:
and when we do String s="abc"; then "abc" is created on heap or constant pool?


In constant poll, b'coz no new keyword !!


I might be wrong here OR couldn't interpreted it correctly, so correct me


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
A string literal is always of type String (§4.3.3. A string literal always refers to the same instance (§4.3.1) of class String.
JLS String Literal

This means String Literals are just references to the instances of class String. It clears that String literal pool contains literals means only references to String instance. So if String s="abc"; is called "abc" will be created as String Instance on the heap and its literal means its reference will be stored in the pool.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Punit Singh wrote:


Can anybody clear the doubt how many objects should be created when String s = new String("abc"); is called ?


When you write String two = new String("someString"); , then two objects will be created in the heap, both having the same value. One will be referenced by the string literal pool, and the other will be referenced by the reference two as shown in the tutorial...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

It means if we create String like
String s=new String("abc");
and if there is not already "abc" String object in heap, then two objects are created, one of "abc" will have reference in pool and another one is referred by s.
But if we have
String t="abc"
String s1=new String("abc"), then this line creates just one object on heap referred to be s1,as one "abc" is already there on the heap whose reference is in the pool already.
Is it right Punit?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
if we have
String t="abc"
String s1=new String("abc"), then this line creates just one object on heap referred to be s1,as one "abc" is already there on the heap whose reference is in the pool already.


It is right.

It means if we create String like
String s=new String("abc");
and if there is not already "abc" String object in heap, then two objects are created, one of "abc" will have reference in pool and another one is referred by s.


I have doubt on this. I think only one object will be created and reference of the same object will be stored in String literal pool also. I cannot confirm this through coding, there is no way to confirm.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

It will create two objects. If you want to check it, then just try this program

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I know Ankit it will print false, but this is not the checking I want, I know what happening here:

String s2 = "abc"; it is created in heap when class is loaded only, so it is already available when class is run by jvm means when String s1 = new String("abc"); is executed at runtime.

see this tutorial about When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. String tutorial

When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals. When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table. Once a reference is made to that String object, any references to that String literal throughout your program are simply replaced with the reference to the object referenced from the String Literal Pool.


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

I still don't get what you want. Are you taking into account this code

String s1 = new String("myString");
String s2 = "myString";

OR this one


String s1 = new String("myString");
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


I am talking about this code. When a .java file is compiled into a .class file, any String literals are noted in a special way, just as all constants are. When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals.When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table. Once a reference is made to that String object, any references to that String literal throughout your program are simply replaced with the reference to the object referenced from the String Literal Pool.

So when this code will be loaded then, line2 String s2="abc"; will be treated as String literal and there is no "abc" instance in the heap already, so JVM will create a new String instance "abc" on the heap and store a reference to that object in the constant table.

And when this class will be run by JVM, line1 will be encountered String s1 = new String("abc"); //1 and when you come to the keyword "new," the JVM is obliged to create a new String object at run-time, rather than using the one from the constant table. So new String("Object") will be created.

So in this code total 2 objects will be created. But line2 String s2="abc"; String Object will created before line1 Object. So they are not equal.
But saying String s1 = new String("abc"); //1 will create 2 objects in the heap is completely wrong na.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

But I think that when the JVM parses the code for string literals, then it will scan the first statement.

String s1 = new String("abc");

It will see that there is a string abc in this statement. So it will create one in the pool. Then it will come to the second statement

String s2 = "abc";

And this time the JVM will take the value from the pool...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
No only string literals are treated in a special way as all constants are. When you are using new String("abc"), this will be executed only at runtime, not at the time of class loading.
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171


When a class is loaded (note that loading happens prior to initialization), the JVM goes through the code for the class and looks for String literals.When it finds one, it checks to see if an equivalent String is already referenced from the heap. If not, it creates a String instance on the heap and stores a reference to that object in the constant table.

So line 1 should create just one object here. As there is already one created on heap (referenced from pool) during class loading!!




Interesting Question!
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


In this case line4 object will be created at class loading time.
And when line1, line2 and line3 will be created at run time and interned at runtime. So it will print true.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

But in this statement new String("abc") the bold text is also a String literal. The difference between

String s1 = new String("abc");

and

String s1 = "abc";

is that in the second one it will assign the pool value to s1. Now let's take another example right from the tutorial that you read

String s = "Hello";
s.concat("World");

Now here you would say that s.concat("World"); will be executed at run time. But the value World will go into the pool. Let me show you a program to confirm it



This code will print true...
Abhishek Bhat
Greenhorn

Joined: Jan 01, 2009
Posts: 25
Hi Punit,

excellent observation there. i'll tel you sumthing, think of it from the perspective of the people who were developing that concept of literal pool and Heap . They definitely did not want to make things complex or introduce redundancy.

Think !

If there's a String "abc" created using the syntax String s = "abc" . then as we know the literal would simply be pushed into the literal pool .

Now follow the String s = new String("abc" ) syntax . what would the concept developer think of it . he mite say "abc" is already there in the literal pool...let me create 2 objects on the heap since there's a <new> keyword and replace the "abc" in the literal pool with the "abc" brought into the heap by new String("abc") .

NOW, if an "abc" is not found in the literal then we know two objects will be created, we know wat "new String() " will do , I will push this new "abc" value into the literal pool .

And as far as references are concerned . correct me if am wrong, The string object " abc" is immutable .. but the value of the REFERENCE can be changed.... so the assignments wil be made depending on how the code follows...


I am really sorry if i was full of errors in my explanation.....but i guess its very logical....i know your way too clear wid it Punit.....Am juz sharing my perspective
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
That means for String s=new String("abc");

If "abc" will be taken by loader at loading time and if it is not in the heap already, then new String instance of "abc" will be created and its reference will be stored in the constant pool. (1st object)

And at runtime JVM is bound to create new String("abc"); as we have used new keyword (2nd object), so total 2 objects will be created.


Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


I have made one more code that is suggesting what Ankit said recently in the last post.
1. here "abc" is created at class loading time in the heap and reference stored in the constant pool.
2. At runtime new String("abc") created, as JVM is bound to do so.
3. b=a.intern(); b gets reference of "abc" from the constant pool.
4. Thats why a==b is false while a.intern()==b is true.

Am I wrong Ankit here?

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Yes Punit. Now you got it. When you write new String("abc"); then that also contains a string literal. So that value will go in the pool. And then a new String will be created at run time. The second example given by you using intern proves this point. You are right on the point. But this doesn't matter. If you write

String s1 = new String("abc");
String s2 = "abc";

Then it doesn't matter which of the literals goes into the pool. However it's good that you are so curious to go into absolute depth

[Edit: Didn't see the second example by Punit]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Thanks, now everything goes into mind, but one thing is contrary that K&B book says the literal "abc" will be placed in the pool. I think here literal means reference of "abc". As "abc" will be created on the heap only.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Punit Singh wrote:Thanks, now everything goes into mind, but one thing is contrary that K&B book says the literal "abc" will be placed in the pool. I think here literal means reference of "abc". As "abc" will be created on the heap only.


The book is designed to be simple in language. If they will say that the string will go into the heap and the pool will have a reference of the string, then some of the people will throw the book into trash
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Good to see a just a deep discussion.
Thanks Punit for curiosity and thanks to Ankit in helping to find the soultion.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes, it was necessary James, String object questions are also one of the most asked questions in this forum. And somewhat it is confusing also. It arises from a different question that was asked Duran Harris here, and Mitesh Soni is my colleague, that leaded to confusion between us, so needed a complete clearance of working of the String literals and Objects. A long discussion will teach you a lot.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

well,it was fruitful.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Very impressive, guys! Thanks for the discussion. It's good to learn something new on a matter you think you already understand.


All code in my posts, unless a source is explicitly mentioned, is my own.
Bindu Lakhanpal
Ranch Hand

Joined: Oct 17, 2008
Posts: 171

Yes, thanks to Punit for raising the doubt and ankit for clearing it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String literal Pool doubt