wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Literal pool again 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 "Literal pool again" Watch "Literal pool again" New topic
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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3018
    
  35

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.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
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
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3018
    
  35

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.
 
wood burning stoves
 
subject: Literal pool again
 
Similar Threads
String doubt
String
Any one explain the following one
String
what is the difference??