File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

G.C. on local variable.........

 
Mellihoney Michael
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";//local variable
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
f) it is hard to say whether After line 11 or line 12
why the answer is e,not f!
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is because the content of variable anObject gets out of scope at line 11!!
the answer could be line 09 only if locObj wasn't assigned for anObject's content!!!
the content of a variable becomes subject of garbage collection if there is no more ways to achieve it!!! In this case there will remain a path to the content that was first assigned to anObject by the local var locObj, when locObj gets out of scope, there will be no more reachable paths!!! hope it helps!!! Correct me if wrong!!!
 
Igor Zeta
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm agree with you
in fact the var is defined
local to the if-block
so its scope ends when the block ends.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the exam there will not be questions about the garbage collection eligibility of objects either:
a) pointed to by local variables that falls out scope
or
b) string objects computed from string literals
Please trust me
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Botella:
In the exam there will not be questions about the garbage collection eligibility of objects either:
a) pointed to by local variables that falls out scope
or
b) string objects computed from string literals
Please trust me

Jose
While, i do agree with your point b), I have had questions on local reference variables and GC when I took the exam
Sri
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it was not a question about falling out of scope, but something about aliasing the same object with several variables; and maybe setting some to null ???
 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I am wondering about the trim methods associated with the objects.
What is their purpose.
Thank you
Pallavi
 
Jimmy Chiu
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mellihoney Michael:
At what point is the object anObj available for garbage collection.

01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";//local variable
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
f) it is hard to say whether After line 11 or line 12
why the answer is e,not f!


Is "sample" created in the pool or on the heap?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic