The code at '1' creates a new String object with the value "summer". This object is not placed in the String pool.
The code at '2' creates a new String object with the value "summer" and places it in the string pool.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
nick woodward wrote:Which is correct? Or have I misunderstood?
nick woodward wrote:Also, on a related topic, if a String becomes unreachable (and was in the String pool) is it eligible for collection, or does it remain in the pool for future use? K&B seems to refer to this as wasted memory, so i thought perhaps strings differed from objects in this way, but can't seem to find
So no need to worry about Strings being eligible for garbage collection or not (for the exam).K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.
Roel De Nijs wrote:
nick woodward wrote:Which is correct? Or have I misunderstood?
This topic discusses exactly the same code snippet. Have a read and let us know if your doubts are cleared.
nick woodward wrote:Also, on a related topic, if a String becomes unreachable (and was in the String pool) is it eligible for collection, or does it remain in the pool for future use? K&B seems to refer to this as wasted memory, so i thought perhaps strings differed from objects in this way, but can't seem to find
Although strings and garbage collection can be lots of fun I like to mention this quite important note (from K&B7, page 201)So no need to worry about Strings being eligible for garbage collection or not (for the exam).K&B7 wrote:Note: Due to the vagaries of the String constant pool, the exam focuses its garbage collection questions on non-String objects, and so our garbage collection discussions apply to only non-String objects too.
Hope it helps!
Kind regards,
Roel
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
nick woodward wrote:So line 1 creates 2 objects, one when the class loads, the other at run time?
nick woodward wrote:I know you said to cut Mala's guide some slack, but as much as I love her chapter on Strings, her explanation does appear to be incorrect. I suppose for the purpose of the exam I can't see how it would make any difference, but I'm not sure why she approached it that way...
nick woodward wrote:*edit: K&B states that the downside to the string object is that memory is wasted via the creation of an object 'lost' in the string pool when a string is altered. But is that not in many cases the same with StringBuilder too?
StringBuilder sb = new StringBuilder("abc"); still creates the string pool object/reference 'abc', which would be 'lost' in the same manner. The text also mentions that only a single stringbuilder object was needed.... which doesn't seem right when their string example explicitly states that two objects were created, the 'new' object, and the literal.
Sachin Tripathi wrote:In your case ,JVM finds "summer" (string literal) at line 2 and put its reference in string literal pool,now as many time this literal arrives in your code,it gets replaced by its reference in string literal pool
Sachin Tripathi wrote:Conclusion: summer and pool both has reference to "summer"(string literal)
summer2 has reference to another object with "summer"
Sachin Tripathi wrote:Now for GC:
Even if you make summer and summer2 null even though string literal pool has reference to one string
So only one object is eligible for GC
Notice that in each of the previous two [stringbuilder] examples, there was a single call to new, so in each example we weren't creating any extra objects. Each example needed only a single StringBuilder object to execute.
nick woodward wrote:but I think this:
Notice that in each of the previous two [stringbuilder] examples, there was a single call to new, so in each example we weren't creating any extra objects. Each example needed only a single StringBuilder object to execute.
is poorly worded given the context.
The single call to 'new' isn't the reason for the lack of an extra object, and wouldn't be the reason for an extra object in a string example (which is what it feels like the implication is). For one thing, there is only a single call to new in that example itself.
Roel De Nijs wrote:
nick woodward wrote:but I think this:
Notice that in each of the previous two [stringbuilder] examples, there was a single call to new, so in each example we weren't creating any extra objects. Each example needed only a single StringBuilder object to execute.
is poorly worded given the context.
The single call to 'new' isn't the reason for the lack of an extra object, and wouldn't be the reason for an extra object in a string example (which is what it feels like the implication is). For one thing, there is only a single call to new in that example itself.
It's probably nothing more but interpretation. In both StringBuilder examples, there was a single call to new, so each example there was just 1 object created, no other extra objects created. Would the same example have been with String, then you would have had many calls to new, but the main problem is: you don't see them, because these calls happen behind the scenes, on each invocation of a method which manipulates the character sequence. But as you know a String is immutable, so if you concat another String, you'll know a new object (String) will be created. But if you append another String to a StringBuilder, it won't result in creating another object.
I think that's the most important thing to get/understand from these examples.
nick woodward wrote:I just thought that as the ambiguity came up in my mind when reading, it might come up in others.
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:my explaination was regarding the reverse sequence (the most frequent way of occurance,of such type question)as actually asked by op
Trying to collect the broken pieces of my life,in the process of making out a beautiful picture out of it.
Sachin Tripathi wrote:No I truly accept my fault,I was saying that the reason of my explanation is just because I was accustomed to reverse sequence of code snippets
Consider Paul's rocket mass heater. |