The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
naved momin wrote:and other Object is eligible for gc
Joanne
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Rob Spoor wrote:"naved" isn't stored on the heap but in the String constant pool.
What is the String Literal Pool? ... it's a collection of references to String objects. Strings, even though they are immutable, are still objects like any other in Java. Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool.
Joanne
Joanne Neal wrote:
Rob Spoor wrote:"naved" isn't stored on the heap but in the String constant pool.
Corey McGlone wouldn't agree with you on that.
What is the String Literal Pool? ... it's a collection of references to String objects. Strings, even though they are immutable, are still objects like any other in Java. Objects are created on the heap and Strings are no exception. So, Strings that are part of the "String Literal Pool" still live on the heap, but they have references to them from the String Literal Pool.
Joanne Neal wrote:
naved momin wrote:and other Object is eligible for gc
No. The other object is referenced from the string pool (because it is a String literal) and so will not be GCed
Pavan Kumar Dittakavi wrote:So, just to make sure, that means that the string "naved" is stored in the string constant pool.
And toUpperCase() method creates a new string object.
So, we have two objects essentially. Is this correct?
Jeff Verdegan wrote:Yes, always
Joanne
Joanne Neal wrote:
Jeff Verdegan wrote:Yes, always
One small exception - if the string is already all uppercase, the original string is returned.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
Jeff Verdegan wrote:
Pavan Kumar Dittakavi wrote:So, just to make sure, that means that the string "naved" is stored in the string constant pool.
Yes, because it's a String literal in the source code.
And toUpperCase() method creates a new string object.
Yes, always, regardless of whether the original was in the constant pool or not, and regardless of whether or not there's a String equal to the result already in the constant pool.
So, we have two objects essentially. Is this correct?
Yes, "naved", which is in the constant pool, and "NAVED", which is not.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
naved momin wrote:
Jeff Verdegan wrote:
Pavan Kumar Dittakavi wrote:So, just to make sure, that means that the string "naved" is stored in the string constant pool.
Yes, because it's a String literal in the source code.
And toUpperCase() method creates a new string object.
Yes, always, regardless of whether the original was in the constant pool or not, and regardless of whether or not there's a String equal to the result already in the constant pool.
So, we have two objects essentially. Is this correct?
Yes, "naved", which is in the constant pool, and "NAVED", which is not.
thanks jeff, lets revise everything
String literals are stored on string constant pool
//this cannot be gced since they are not in the heap, ok ?
String object that we get after envoking any of String's methods are stored in heap
when the String literal is exactly same as the returning String object then the literal from the constant pool is returned,
so no new object is created in this case right ?
Jeff Verdegan wrote:
naved momin wrote:
Jeff Verdegan wrote:
Pavan Kumar Dittakavi wrote:So, just to make sure, that means that the string "naved" is stored in the string constant pool.
Yes, because it's a String literal in the source code.
And toUpperCase() method creates a new string object.
Yes, always, regardless of whether the original was in the constant pool or not, and regardless of whether or not there's a String equal to the result already in the constant pool.
So, we have two objects essentially. Is this correct?
Yes, "naved", which is in the constant pool, and "NAVED", which is not.
thanks jeff, lets revise everything
String literals are stored on string constant pool
Correct.
//this cannot be gced since they are not in the heap, ok ?
According to the JVM spec: "Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it."
The constant pool is part of the method area, which is logically part of the heap, and individual implementations may choose to GC or not GC it.
String object that we get after envoking any of String's methods are stored in heap
All objects are always stored in the heap. (Except that there's been talk of allowing stack storage for objects that don't leak out of the method that created them. I don't know if it's been implemented yet.)
when the String literal is exactly same as the returning String object then the literal from the constant pool is returned,
so no new object is created in this case right ?
String literals have nothing to do with it. It's when the result of calling a method is a String that is equal() to the original, then no new String is created. So, for instance, "naved".toUpperCase() will create a new String object and return a reference to it, whereas "NAVED".toUpperCase() will simply return a reference to the original "NAVED" String. Note that this is true whether the original is a literal or not, and whether it is stored in the constant pool or not.
So:
1. There's a constant pool.
2. It's a special part of the heap.
3. It may or may not be GCed, and you really never need to worry about whether it is or not.
4. Any String literal in your source code, such as "naved", is stored in the constant pool.
5. Any time you call intern() on a String, a new entry is put into the constant pool, if there's not one already there that is equal() to the original.
6. The JVM stores certain Strings in the constant pool, such as class names and method names. This is not something you need to be concerned about when writing Java code.
7. When the result of a String method would be equal() to the original String, there is no new String object created; it simply returns a reference to the original (that is to itself). This is completely independent of the constant pool and has nothing to do with the constant pool.
The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/