String
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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...?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 ?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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.
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
rgrds
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 |








