*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion on the topic of String constant table ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion on the topic of String constant table ?" Watch "Confusion on the topic of String constant table ?" New topic
Author

Confusion on the topic of String constant table ?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

I was reading about the string constant table from kathy Sierra scjp 6 book . I would like to quote a paragraph from the book as shown below

Earlier we promised to talk more about the subtle differences between the various
methods of creating a String. Let's look at a couple of examples of how a String
might be created, and let's further assume that no other String objects exist in the
pool:
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.


It says that the "abc" will go in the pool and s will refer to it. whereas the article at says that objects will be created in heap and the reference will go in to the string constant table. following is the paragraph from the article

hen 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 exactly who is write ???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

gurpeet singh wrote:I was reading about the string constant table from kathy Sierra scjp 6 book . I would like to quote a paragraph from the book as shown below

Earlier we promised to talk more about the subtle differences between the various
methods of creating a String. Let's look at a couple of examples of how a String
might be created, and let's further assume that no other String objects exist in the
pool:
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.



It says that the "abc" will go in the pool and s will refer to it. whereas the article at says that objects will be created in heap and the reference will go in to the string constant table. following is the paragraph from the article

hen 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 exactly who is write ???



I think that you may be reading it too literally. The string constant pool only contains references, so when discussing that "abc" will be placed in the pool, the book is inferring that a reference to an "abc" object has been added. Both are saying the same thing -- except that the later is giving more details.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

thanks henry...but the book says that when we do it creates a new object in normal(non-pool) memory. Referring to the paragraph i quoted from the book dont you think the book authors are like contrasting from the case String x ="abc"; by saying that former syntax creates object in non-pool memory whereas later will be created in string constant pool. there is nowhere the mentioning of the heap , where all the objects are created. In addition the book says when we do abc will go in the pool and 's' will refer it.

Maybe you are right i'm misinterpreting and reading the book too literally but i m unable to get it off my head and proceed further unless i'm clarified.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
I think




Try this to see the result:


Or , do this to see the result:


I hope it helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion on the topic of String constant table ?
 
Similar Threads
String literal Pool doubt
creating new Strings(from K&B)
Objects created...
what is the difference??
String and String Object Problem