• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Brogden's Java 2 Exam Cram, Chapt 8 Question 7

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void countDown{
for (int i = 10; i >= 0; i--){
String tmp = Integer.toString(i);
System.out.println(tmp);
}
System.out.println("BOOM!");
}
When the program reaches line 6, how many objects created in line 6 are eligible for garbage collection? Assume that System.out object is not keeping a reference.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I missed on this question, did not observe it carefully that toString() returns a String object. I think, 10 objects are eligible to be gc'ed as the last one (11th object) still has a reference(tmp).
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, the following is beyond the scope of the certification exam, so don't worry if this seems to nit-picky - you can safely ignore it if certification is your only goal.
The local variable tmp is out of scope by line 6, so the 11th object is eligible for collection as well. This question has come up before, and I'm annoyed I can't find the previous topic where it came up, or I'd refer you there. The question is from Bill Brogden's book, right? He says the answer is 11, and elaborates on his reasoning here. But Tony and I disagree with his reasoning. Our position is that even if a particular implementation of Java holds on to the tmp reference after it's out of scope, so what? According to the language specs, Java can discard tmp as soon as it's out of scope, and therefore the 11th object is eligible for collection at that point even if a particular implementation will never successfully do so. Through testing I've determined that jdk 1.2.2 for Windows can only collect 10 objects at line 6, but jdk 1.3 beta for Windows can collect all 11. I view this as a bug in 1.2.2 that has been fixed.
From an exam perspective also, I'd say the answer should be 11 simply because Brogden's argument for 10 (and my counterargument for 11) are outside the scope of what you're expected to know for the exam. The naive interpretation is that tmp is out of scope, so all 11 are eligible for collection. But that's just my opinion, of course.
 
sreelakshmi sarma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above code isn't the "tmp" variable refering to string literals.Then howcome the literals will be eligible for GC in the first place.Jim i really need your help here.
Thankx.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. A string literal is when you actually see a pair of double quotes in the code, and the exact contents of the string are spelled out for you - like if "10","9", "8", etc. had appeared in the code. What we have instead is another example where even though there's no "new", we've created a string that is not part of the interned string pool. The method Integer.toString(i) returns a newly created String that is 1) not a literal, 2) not the result of a constant expresstion since i is not a constant, and 3) not the result of the intern() method. So it's not part of the intern pool, and it can be collected.
 
bob
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand how it is 11. Isn't the string reference tmp being overwritten in the for loop. Can someone explain please.
 
nirvan sage
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bob:
I cannot understand how it is 11. Isn't the string reference tmp being overwritten in the for loop. Can someone explain please.

I think you just stated your answer.Since all the references to the objects being created are being lost,these objects become eligible for garbage collection.Finally it becomes a matter of whether 11 objects or 10 objects are being collected,I think
Jim has given an apt answer for this one :-)

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm adding a new message just so this thread shows up as "recent" and people can see it...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic