• 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

GC question

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I found this question related to garbage collection.can anyone tell what the output of this code would be

public static void main(String args[])
{
Double x= new Double(0);
for(int i=0;i<5;i++)
for(int j=0;j<3;j++)
x = new Double(i+j)
int a = x.intvalue();
System.out.println("a = +" a);
}
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. This code snippet has few syntax issues:
x = new Double(i+j).........should end with ";"
int a = x.intvalue();.......it is intValue();
and the print statement should be...System.out.println("a = "+ a);
2. Since
x = new Double(i+j);
is in the loop, every loop iteration creates a new Double object. At the end of the loop, what remains in x is the last Double object created (where i will have the max value possible with the outer loop (4), j will have the max value possible with the inner loop (2). Thus, your program will output "a = 6".
3. Now, since you mention the question is related to GC, the question, I think, can't be just "what is the output?". It may be something related to the fact that each time you go through the loop, you loose the reference to the Double object created during the previous iteration - thus making them possible for GC. All in all you will have 15 Double objects that won't have any reference at the end of the loop, and they will all be eligable for GC.

Hope this helps.
 
Preetha Vasudevan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks jairam for your prompt reply but i might sound stupid ..
how 15 objects will be eligible for garbage collection after the end of the main method??
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Preetha,

See, there in the code we actually create 16 Double objects.(1 initially + 15 in loop). Once we complete the loop..last object generated will be pointed by "x" which wont be eligible for garbage collection.

So, 15 Objs will be eligible for GC.

Thanks and Regards,
Mausam
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preeta,
At the end of main method all the 16 objects will be eligible for GC. Since x is a local variable to main mehod, after the end of main the last object generated will also be eligible for GC.

Thanks,
Prashanth.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the 15 objects created in 15 iterations, each object has only one refernece and it looses the same in next iteration (and become eligible for Gc)
 
reply
    Bookmark Topic Watch Topic
  • New Topic