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

String literal Pool doubt

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will create two objects. If you want to check it, then just try this program

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good to see a just a deep discussion.
Thanks Punit for curiosity and thanks to Ankit in helping to find the soultion.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well,it was fruitful.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very impressive, guys! Thanks for the discussion. It's good to learn something new on a matter you think you already understand.
 
Bindu Lakhanpal
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, thanks to Punit for raising the doubt and ankit for clearing it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic