Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Garbage Collection and Post Increment Operator doubts

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!



 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 6888
1312
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic