• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about gc

 
michael wang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0

i think the answer is D
but the answer given is A
why?
thanks
 
William Ulman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Test" needs to be gc'ed?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a terrible question. First of all, you NEVER can make predictions about what is going to be garbage collected or when it will happen. The best you can do is say when an object is eligible for garbage collection.
Second, string literals are never garbage collected. The string literals "Test" and "Today" will not be garbage collected. Since there are no other objects being created in the sample code (ie, there is no "new" operator) there will be no objects eligible for garbage collection.
What test is this question from?
 
michael wang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Ross:
This is a terrible question. First of all, you NEVER can make predictions about what is going to be garbage collected or when it will happen. The best you can do is say when an object is eligible for garbage collection.
Second, string literals are never garbage collected. The string literals "Test" and "Today" will not be garbage collected. Since there are no other objects being created in the sample code (ie, there is no "new" operator) there will be no objects eligible for garbage collection.
What test is this question from?

this question is from Javacaps (NO 12 Question)
 
Junilu Lacar
Bartender
Pie
Posts: 7747
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At least you've shown that you learned something by questioning the validity of the answer given .
Junilu
[ March 05, 2002: Message edited by: Junilu Lacar ]
 
michael wang
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Junilu Lacar:
At least you've shown that you learned something by questioning the validity of the answer given .
Junilu
[ March 05, 2002: Message edited by: Junilu Lacar ]

surly
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic