Note that there are some explanatory texts on larger screens.

plurals
  1. POIs String Literal Pool a collection of references to the String Object, Or a collection of Objects
    primarykey
    data
    text
    <p>I am all confused after reading the article on javaranch site by Corey McGlone, The author of The SCJP Tip Line. named Strings, Literally and the SCJP Java 6 Programmer Guide by <code>Kathy Sierra (co-founder of javaranch) and Bert Bates</code>.</p> <p>I will try to quote what Mr. Corey and Ms Kathy Sierra have quoted about String Literal Pool.</p> <p><strong>1.</strong> According to Mr Corey McGlone :</p> <ul> <li><p>String Literal Pool is a Collection of references that points to the String Objects.</p></li> <li><p><code>String s = "Hello";</code> (Assume there is No object on the Heap named "Hello"), will create an String object <code>"Hello"</code> on the heap, and will place an reference to this object in the String Literal Pool (Constant Table)</p></li> <li><p><code>String a = new String("Bye");</code> (Assume there is No object on the Heap named "Bye", <code>new</code> operator will oblige the JVM to create an object on the Heap.</p></li> </ul> <p>Now the explanation of <code>"new"</code> operator for the creation of a String and its reference is bit confusing in this article, so i am putting the code and explanation from the article itself as it-is below.</p> <pre><code>public class ImmutableStrings { public static void main(String[] args) { String one = "someString"; String two = new String("someString"); System.out.println(one.equals(two)); System.out.println(one == two); } } </code></pre> <p>In this case, we actually end up with a slightly different behavior because of the keyword <code>"new."</code> In such a case, references to the two String literals are still put into the constant table (the String Literal Pool), but, when you come to the keyword <code>"new,"</code> the JVM is obliged to create a new String object at run-time, rather than using the one from the constant table.</p> <p>Here is the diagram explaining it..</p> <p><img src="https://i.stack.imgur.com/8FAKX.jpg" alt="enter image description here"></p> <p><strong>So does it mean, that String Literal Pool too have a reference to this Object ?</strong></p> <p>Here is the link to the Article by Corey McGlone</p> <p><a href="http://www.javaranch.com/journal/200409/Journal200409.jsp#a1">http://www.javaranch.com/journal/200409/Journal200409.jsp#a1</a></p> <p><strong>2.</strong> According to Kathy Sierra and Bert Bates in SCJP book:</p> <ul> <li><p>To make Java more memory efficient, the JVM set aside a special area of memory called the "String constant pool", when the compiler encounters a String Literal, it checks the pool to see if an identical String already exits or not. If not then it creates a new String Literal Object. </p></li> <li><p><code>String s = "abc";</code> // Creates one String object and one reference variable.... </p> <p>thats fine, but Now the below statement got me confused.</p></li> <li><p><code>String s = new String("abc")</code> // Creates two objects, and one reference variable.</p> <p>It says in the book that.... a new String object in normal(non-pool) memory , and "s" will refer to it... whereas an additional the literal "abc" will be placed in the pool.</p> <p>The above lines in the book collides with the one in the article by Corey McGlone.</p> <ul> <li><p>If String Literal Pool is a collection of references to the String object as mentioned by Corey McGlone, then how come literal object "abc" will be placed in the pool, as mentioned in the book.</p></li> <li><p>And where do this String Literal Pool resides.</p></li> </ul></li> </ul> <p>Please clear this doubt, though it won't matter too much while writing a code, but is very important from the aspect of memory management, and thats the reason i want to clear this funda.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload