I am new to java and I am getting confused in one statement on Garbage collector. Please help me with this.
So, I wanted to know that, when we did a2 = null on Line 1, so I just wanted to confirm that a2 object becomes eligible for Garbage collection on the same line or when the code flow goes to Line 2. Please help me with this, it is creating a confusion in my mind.
Not something you need to know to program Java®, only to pass a cert exam. If you read something elsewhere, please tell us where it was.
There is no such thing as an object a2, but there is a reference a2 which points to an object. The object behind that reference becomes eligible for GC when line 9 (what you called // line 1) completes. What difference is there between the timings of the two lines you showed?
Thank you Campbell Ritchie for the reply. Actually I am giving Java 8 OCA exam this week and while practicing the mock test exam given in Mala Gupta OCA 8 book, I came across this question. That is why I wanted to know that if the object referred by a2 will become eligible for GC at Line 1 or when code reaches Line 2. I know in real world programming it might not be of that significance, however from Certification point of view, I have to prepare for every single aspect.
a At least two objects are garbage collected on line 1.
b At least one object is garbage collected on line 1.
c No objects are garbage collected on line 1.
d The number of objects that are garbage collected on line 1 is unknown.
e At least two objects are eligible for garbage collection on line 2.
This was the actual question and answer was Option: - d,e
So after visiting this question, this thing strikes my mind that what if the examiner asked specifically that object referred by a2 will become eligible for GC at Line number 1 or not ?
And I am beginner with java, so please ignore if I made any wrong statement,
That is different from what you posted at first. It is most likely that no GC occurs at all; there is not enough heap use to make it worthwhile. You do not therefore know whether anything will be GC‑ed on // line 1 (d) and the objects behind a1 and a2 are unreachable by // line 2 (e).
Since the behaviour of the GC is not predictable, none of a, b, and c can be correct; they are all mutually incompatible with d.
Moving to our OCA forum. If you really are a beginner, I suggest you delay the exam. You should be getting 70%+ regularly on the mock tests before sitting the real thing.
Thats true, even if we call GC it is not compulsory that GC will run. I was asking in a hypothetical way, that suppose GC runs, then object referred by a2 will become eligible for GC at the same line (// line 1) or when the compiler reaches next line.
That is what d and e mean; you don't know whether the GC will run at all, but if it does run it will delete the object behind a2 only. As I said, the object behind a2 becomes eligible for GC when // line 1 completes, i.e. the end of // line 1.
Additionally, the words "on line X" are problematic.
I think the better quality Exam Preparation materials say "after line X" or "after line X" completes.
The term "on line X" is fine for referring to syntax errors or scope, but not for runtime analysis.
RTFJD (the JavaDocs are your friends!) If you haven't read them in a long time, then RRTFJD (they might have changed!)
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad: