| Author |
Literal pool again
|
Vaclav Burda
Greenhorn
Joined: Jun 07, 2009
Posts: 3
|
|
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.
|
http://zerog.blog.cz/ (only in Czech language)
And sorry for my English ;)
|
 |
Devaka Cooray
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
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.
|
Author of ExamLab (Download) - the free mock exam kit for SCJP / OCPJP
Home Page -- Twitter Profile -- JavaRanch FAQ -- How to Ask a Question
|
 |
Vaclav Burda
Greenhorn
Joined: Jun 07, 2009
Posts: 3
|
|
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
Saloon Keeper
Joined: Jul 29, 2008
Posts: 2691
|
|
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
Joined: Aug 31, 2004
Posts: 11343
|
|
|
Vaclav, it might be nice to cite the source of your graphic. This is from Corey McGlone's article, Strings, Literally.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Vaclav Burda
Greenhorn
Joined: Jun 07, 2009
Posts: 3
|
|
Devaka thank, I understand.
marc weber thank for your comment.
|
 |
 |
|
|
subject: Literal pool again
|
|
|