aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC problem" Watch "GC problem" New topic
Author

GC problem

vineet walia
Ranch Hand

Joined: Feb 16, 2008
Posts: 138

how many object are eligible for GC???
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

I think four objects are eligible for GC.


Regards
Balraj Kumar
SCJP 5 95%
SCWCD 82%
Preparing for SCBCD
Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
3 objects:



[SCJP 6.0]
Balraj Momi
Ranch Hand

Joined: Jul 23, 2009
Posts: 45

Anastasia is right, there are 3 objects eligible.
It will be 4 If gb1 is assigned to null.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Vineet walia says
how many object are eligible for GC???


I guess you should draw a diagram and first give your answer so that you can verify with the ranchers.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Number of objects actually created - Number of objects still having references assigned


Experience and talent are independent of age
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

vineet please Quote Your Sources when you post a question. You've posted multiple questions without a proper source. Your questions will be deleted if you don't give the source properly...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

This is a famous examlab question....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Nitish Bangera wrote:This is a famous examlab question....


Its good that it is from a legal mock exam, but generally we expect the topic creator himself/herself to specify the source. That way its easier for everyone. Anyways its good that you are making efforts
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
3 objects ,right?


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Its easy to find the number of object eligible for GC using a diagram

At line 7, the memory map looks like



At line 8 it becomes



at line 9 it becomes



and at line 10 it is



So 3 objects are eligible for GC (The one with the capital G in them)...
vineet walia
Ranch Hand

Joined: Feb 16, 2008
Posts: 138
Anastasia Sirotenko wrote:3 objects:



bro not geting your handy notation can you exp lain little bit??
vineet walia
Ranch Hand

Joined: Feb 16, 2008
Posts: 138
Ankit Garg wrote:Its easy to find the number of object eligible for GC using a diagram


thank Ankit Garg i thnk i will help me out

[Edit AG: Removed long quotes which were looking messy]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

@vineet, you can use the @<NAME> to direct your message to someone specific instead of quoting whole posts. Like what you said in the last two posts can be done like -

@Anastasia, bro not geting your handy notation can you explain little bit??

@Ankit, thanks I thnk I will help me out

That way its easier to read as quoting long posts can look very messy...
Tanya Shetty
Ranch Hand

Joined: Jun 17, 2009
Posts: 40
Can somebody tell me why this line No. 7



does not give a null pointer exception, since, ultimately it does try to cast a null type to GARB type in the constructor..

I have tries this code and it runs fine!

Also, if i replace line no. 7 by,



I DO get the null pointer exception , why is this ???

Anastasia Sirotenko
Ranch Hand

Joined: Jul 20, 2009
Posts: 64
NullPointer comes from line 8, where you try to instantiate gb1.g.g , tho gb1.g is null after line 7
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Tanya this is from the JLS

The null reference can always be cast to any reference type.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Just check where exatly you get the exception, and you shall know
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Ankit that was a GREAT diagram.
Absolutely wonderful. I wonder how long it took you to draw it out.

The first time I answered this question I drew the diagram but I still got the answer wrong. I drew an extra object becuase I was confused.

Then I reviewed/drew the diagram again before looking in the forums for more explaination.

When I reviewed it for the 2nd time, I got the answer right. and double checked the forum.

The trick with all of these GC questions is that the class always HAS-A instance variable of some object type, and that can be confusing.

The best thing you can do is keep practicing and draw it out.


SCJP 6.0
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: GC problem