• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion on the topic of String constant table ?

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think




Try this to see the result:


Or , do this to see the result:


I hope it helps.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic