programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Jeanne Boyarsky
• Liutauras Vilda
Sheriffs:
• Tim Cooke
• Bear Bibeault
• paul wheaton
Saloon Keepers:
• Carey Brown
• Stephan van Hulst
• Tim Holloway
• Mikalai Zaikin
• Piet Souris
Bartenders:

# Garbage Collection and Post Increment Operator doubts

Greenhorn
Posts: 2
• Number of slices to send:
Optional 'thank-you' note:
Hi,

i am taking preparations for SCJP and of course the book written by Sierra-Bates is my guideline .

I was going through mock SCJP questions on the net and came across these.

Question 1.

Here the answer given is "The objects referenced by a and b are eligible for garbage collection."

Can someone please explain the code to me (in details) and explain why object referenced by aa is not eligible for garbage collection?

Question 2.

Given the code. What is the result?

But i think the answer should be 12.

author
Posts: 23939
142
• Number of slices to send:
Optional 'thank-you' note:

Abhisek Ohdedar wrote:
Question 2.

Given the code. What is the result?
int i = 10;
while (i++ <= 10) {
i++;
}
System.out.print(i);

But i think the answer should be 12.

Take a few seconds, and try it out first...

Henry

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:

Abhisek Ohdedar wrote:Hi,

i am taking preparations for SCJP and of course the book written by Sierra-Bates is my guideline .

I was going through mock SCJP questions on the net and came across these.

Question 2.

Given the code. What is the result?
int i = 10;
while (i++ <= 10) {
i++;
}
System.out.print(i);

But i think the answer should be 12.

It seems I can only provide answer to Question 2.

Ok, let's look at it together:

means the <= condition is tested before the incrementing takes place.

So, the condition is true then i becomes 11
B'cos the condition is true, the inner i++; turns i to 12

Now to the main issue
The system returns to continue with the while loop, tests the condition, discovers it's false, increments i, then leaves the loop to print out the value of i
Thereby making i = 13!

Ranch Hand
Posts: 300
• Number of slices to send:
Optional 'thank-you' note:

Hi Abhishek,

In first question both A and B objects has cyclic dependency on each other , so if you make one object null and then other also become orphan and eligible for garbage collection , read java spec to understand in detail.

Thanks
Javin.

Sheriff
Posts: 7094
1345
• Number of slices to send:
Optional 'thank-you' note:
Welcome to JavaRanch, Abhishek.

Whenever you post a question from a mock exam, we require you to QuoteYourSources. Can you please let us know where you got this question from?

And please don't forget to UseCodeTags and UseOneThreadPerQuestion when you post again. I have added the code tags on your post for you.

Greenhorn
Posts: 15
• Number of slices to send:
Optional 'thank-you' note:
Hi Abhisek,
When I saw your question (first part) I was puzzled too, and added some code to help me figure it out:

Here's the output I got when I ran it:

Created an A: A@12dacd1 Instance 1
Created a B: B@10385c1 Instance 1
set this.a: A@12dacd1
set this.b: B@10385c1
set aa to: A@12dacd1
Is aa == aa.b.a? true
set aa to null.
Nudged garbage collector.
Finalizing a B B@10385c1
Finalizing an A A@12dacd1

You can see that only 1 instance of A and 1 instance of B were instantiated, so there are only two objects to garbage collect after the aa reference to the instance of A is set to null in the GCTest main() method. When the A() constructor is called, it passes a reference to itself to the B(A a) constructor (this.b = new B(this);) and then the B constructor sets its internal A reference to point to the same object as the aa reference in the GCTest main method. So, before aa is set to null there is 1 A object with two references and 1 B object with 1 reference. From the output from the A and B finalizers, you can see that just 2 objects end up being gc'd.

Happy New Year and Regards,
Frank

 Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad: Low Tech Laboratory https://www.kickstarter.com/projects/paulwheaton/low-tech-0