File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection and Post Increment Operator doubts

 
Abhisek Ohdedar
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Here answer given is 13.

But i think the answer should be 12.

Can anyone please explain the answer?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhisek Ohdedar wrote:
Question 2.

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

Here answer given is 13.

But i think the answer should be 12.

Can anyone please explain the answer?


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

Henry
 
Ebenezer Olanrewaju
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);

Here answer given is 13.

But i think the answer should be 12.

Can anyone please explain the answer?


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!



 
Javin Paul
Ranch Hand
Posts: 295
Eclipse IDE Firefox Browser Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4119
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Callahan
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic