aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ch-assignment in kb ,need explanation of output:question 10 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 "ch-assignment in kb ,need explanation of output:question 10" Watch "ch-assignment in kb ,need explanation of output:question 10" New topic
Author

ch-assignment in kb ,need explanation of output:question 10

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


objects eliginle for garbage collection when line 12 is reached?
output:
five objects were created
and two objects are eligible for garbage collection.(second Dozens object and int array object)

please anyone explain me the answer how 5 objects are created here and why da[1] is eligible and why d is not?

SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

here in this program we will go step by step
here is the code

this create an array that is able to store 3 objects of type Dozens.
Remember that an array is always an object in java and here only one object is created and that is also of type Array
the three array elements can store objects of type Dozens but we have not created Dozen objects yet

This creates new Dozen object and now int[] dz is the instance member of class Dozens
so whenever we are creating Dozen object, in[] object is also getting created

again same, two a dozen object is created which has instance member int[] object
hence 2 more objects are created and the total number becomes 5 objects



now look into the code for terse explanation

hope this helps
Happy to help
happy preparation


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


da[1]=null; // here we make da[1] = null so the reference to the Dozen object is lost and also the in[] object in Dozen object is lost
hence two objects are eligible for garbage collection


thanks for wonderfull explanation ,one more thing
hence two objects are eligible for garbage collection
1. int [] object
2. is d or da[1] (because da[1] is never a object it was a reference to d),please clear this thing also??
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

da[1] is not object and d is not an object also
even d is not the object
they both are the reference variables
and also
if there is some statement like

then do not assume that da[1] is referring to d
that means that
THIS IS CORRECT
da[1] is referring to the object that is referred by the reference variable d

IT IS NOT
da[1] is referring to d

above statement is completely wrong

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

thanks sir ,it really helps!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ch-assignment in kb ,need explanation of output:question 10