• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

one more question in GC

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it from

http://www.danchisholm.net/oct1/mybook/chapter16/exam1.html
class B {
private String name;
public B(String name) {this.name = name;}
public String toString() {return name;}
protected void finalize() {System.out.print(name);}
}
class H {
static B ba = new B("Ba");
static int i = 1;
static B m1(B b) {return b = new B("B" + i++);}
public static void main (String[] args) {
B x = m1(ba); m1(x);
System.out.println(", " + ba + ", " + x);
}}

Which of the following could be a result of attempting to compile and run the program?

a. Ba, B1, B2
b. B1, Ba, B2
c. , Ba, B1
d. B2, Ba, B1
e. BaB1b2, null, null
f. B1B2, ba, null

Answer is c, d

here i can understand option c.
But for option d, i assume that method finalize will run even if we dont call System.gc. But If I assume so then why option a and b are not correct because it may run in any order(for any object).please clear my doubt

Thanks,
Geeta Vemula
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only time prior to the end of the program where an object is eligible for GC is when we call m1(x); This triggers .. return b = new B("B" + i++) to be executed. i.e B2, and since that new object is returned to m1(x) but not assigned to a reference, that new object is eligible. This means the GC may or may not run before the very last println in main.
 
Erez Pitchon
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other note, I suppose this means that after the last println statement in main, the GC can't run and execute finalize because at this point the entire program will be unloaded.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why option a and b are not correct



Option b. B1, Ba, B2

the order cannot be acheived.
it must be XXX, Ba, B2 or , Ba, B2XXX


Option a. Ba, B1, B2
the comma after B1 is nowhere printed.

I came to this kind of conclusion because in the last two option they have given answers without space or comma. So it means they are giving importance even for comma and space.

Hope i am correct, if not please correct me.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class H {
static B ba = new B("Ba");
static int i = 1;
static B m1(B b) {return b = new B("B" + i++);}
public static void main (String[] args) {

B x = m1(ba);


when you call x=m1(ba), it made new B("B1");
so x=B(name="B1");



m1(x);


When m1(x) is get called it will make new B("B2") as it is returning
new B("B"+i++) and i is 2 here.
Means it made a B(name="B2"), but we did not hold this returned reference.
So it is now elligible for garabage collection from here now.
And suppose finalize() get called.
protected void finalize() {System.out.print(name);}
it will print this object's name that is: "B2".


System.out.println(", " + ba + ", " + x);

now here in println ba=B(name="Ba") so always second statement after "," will be "Ba", and x=B(name="B1") so always output will be "B1" always after "Ba". so ", Ba, B1" output is must.
If "B2" garbage collected before println() then output will be "B2, Ba, B1".
If "B2" garbage collected after println() then output will be ", Ba, B1B2".
If "B2" not garbage collected then output will be ", Ba, B1".


}}


Got ?
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks james and punit. i got it.. you guys are doing great job.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am wondering how an object can be created as "static" here??

static B ba = new B("Ba");
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here reference variable is static only. So its life time will be equal to class life time.
 
reply
    Bookmark Topic Watch Topic
  • New Topic