• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks sir ,it really helps!!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic