• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

String

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to KS & BB book:"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."

What I have concluded from this is:
1> String Pool is also a part of heap reserved for string literals.
2> And when we create a string object using the keyword new, it appears on heap(which the book has referred as normal or nonpool memory) but not in the String pool.

Am I right? Please correct if I have concluded something wrong.

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:
What I have concluded from this is:
1> String Pool is also a part of heap reserved for string literals.


Correct, String Literal Pool is a part of the heap.

swaraj gupta wrote:
2> And when we create a string object using the keyword new, it appears on heap(which the book has referred as normal or nonpool memory) but not in the String pool.

Am I right? Please correct if I have concluded something wrong.


Not exactly. If you use new to create a String object, first, the JVM check whether the String literal is already on the pool, if not it crates a literal on the heap. But the reference will refer the object which were created in the normal heap. If you create a String object without new, then the the variable will refer the object which were created in the String literal Pool.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Not exactly. If you use new to create a String object, first, the JVM check whether the String literal is already on the pool, if not it creates a literal on the heap. But the reference will refer the object which were created in the normal heap.



I didn't get this point of yours.....whether the String literal is already on the pool, if not it creates a literal on the heap(where in one pool or in non-pool portion and what if the same string literal is already in the pool and we are using the keyword new). ...... Would you please explain in terms of only pool and non-pool portion of heap...?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's fine, we will have some code examples.


With the above code, the JVM, first check whether the String literal "ABC" is in the String Literal Pool, if it already there, then, JVM creates a new String object on the normal heap, the variable s will it. If the String Literal Pool, doesn't contains the String literal "ABC" before, the the JVM, first creates one on the Pool, and then creates String object on the normal heap and the variable will refer the normal heap object. But,

JVM will create a String Literal on the String Literal Pool and the variable will refer it.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks abimaran, got your point.. Now if i add one more statement as:
Now in this case only one String literal i.e. "ABC" will be added to the String pool and this would be when the first statement will execute(if there exists no string literal with the same pattern). And two String objects will be created on non-pool part of heap. Am I right this time ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome. And, for further details, have a look this article.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:thanks abimaran, got your point.. Now if i add one more statement as:
Now in this case only one String literal i.e. "ABC" will be added to the String pool and this would be when the first statement will execute(if there exists no string literal with the same pattern). And two String objects will be created on non-pool part of heap. Am I right this time ?


Correct, you've got it.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot Abimaran..
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Again, welcome!
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

swaraj gupta wrote:thanks abimaran, got your point.. Now if i add one more statement as:
Now in this case only one String literal i.e. "ABC" will be added to the String pool and this would be when the first statement will execute(if there exists no string literal with the same pattern). And two String objects will be created on non-pool part of heap. Am I right this time ?


Correct, you've got it.



I get from this that actually we get three String objects, one in the String pool and two normally created objects. My questions is: the refrence variables s and s1, are refeering to which objects? to the one in the pool or to the nrmally created objects?
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here both s and s1 are referring to the normal objects in the heap (not to the one in pool).
But i think when we write as: then in that situation only when we assign a String literal to a reference variable, the reference variable refers to pool...

Imad Aydarooos wrote:

Abimaran Kugathasan wrote:

swaraj gupta wrote:thanks abimaran, got your point.. Now if i add one more statement as:
Now in this case only one String literal i.e. "ABC" will be added to the String pool and this would be when the first statement will execute(if there exists no string literal with the same pattern). And two String objects will be created on non-pool part of heap. Am I right this time ?


Correct, you've got it.



I get from this that actually we get three String objects, one in the String pool and two normally created objects. My questions is: the refrence variables s and s1, are refeering to which objects? to the one in the pool or to the nrmally created objects?

 
Imad Aydarooos
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no Swaraj ... remeber when you use new keyword you are actually creating new String objects and by the assignment you are binding them to your refrence variables not the pool object, so s and s1 are pointing to the normally created objects
rgrds
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have edited my last post now..

Imad Aydarooos wrote:no Swaraj ... remeber when you use new keyword you are actually creating new String objects and by the assignment you are binding them to your refrence variables not the pool object, so s and s1 are pointing to the normally created objects
rgrds

 
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Imad Aydarooos wrote:no Swaraj ... remeber when you use new keyword you are actually creating new String objects and by the assignment you are binding them to your refrence variables not the pool object, so s and s1 are pointing to the normally created objects
rgrds



Agreed. There is currently no optimization, whether at the compiler or JVM level, that will ignore calls to the new operator and replace it with objects already in the string pool. If you use the new operator, you will get a new object.

Henry
 
Wait for it ... wait .... wait .... NOW! Pafiffle! A perfect tiny ad!
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic