Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Literal pool again

 
Vaclav Burda
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have simple question.


This code create a new instance of String class and put at heap (in non-pool memory).

What is in literal pool at this monent? Is empty or have poiner to the heap, where is instance of String?

Thank and sorry for my english.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaclav Burda wrote:What is in literal pool at this monent? Is empty or have poiner to the heap, where is instance of String?


It is neither empty nor referring to the heap. Look carefully at the instantiation line. For the constructor of the String, you passed a String literal value, "Hello", which will be pooled. But the variable str1 is still referring to the String object in the heap.

Typically the String pool can never empty. It may contain *many* of string values including strings created by the class loader process.

Devaka.
 
Vaclav Burda
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, and which relation is between "Hello" from literal pool and str1, which referring to the String object in the heap?



graphically:




You say:
For the constructor of the String, you passed a String literal value, "Hello", which will be pooled.


but in the picture, second object hasn't line between pool and this object.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String pooling technology is designed to reuse a same String value again and again without creating new objects in heap. In your second program, there are two statements. First statement pools the string value "someString" in the pool. So, no need to pool the same string *again*, because it is existing in the String pool, and it can be reused from the pool. For that reason, the argument you passed to the constructor at the second statement will NOT be pooled. Instead of that it gets the previously pooled value from the pool, and it is used as the argument to construct the NEW string object on the heap.

Devaka
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vaclav, it might be nice to cite the source of your graphic. This is from Corey McGlone's article, Strings, Literally.
 
Vaclav Burda
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devaka thank, I understand.

marc weber thank for your comment.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic