• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Garbage collection

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This is one of the questions from Dan's mock exams

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);
}}

The answers are given as ,Ba, B1 or B2, Ba, B1.

But my question is on the second call to method m1 with an argument x, the return type is of Object B. There should be a reference to B in the main function which can take the return value. calling just m1(x) will cause compiler error, as the function m1 returns an object.

Please clarify me if I am wrong.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sharanya,

Thank you for using my exam.

Ignoring the information returned by a method does not generate a compile-time error.

I know that my answer does not provide a lot of information, so please feel free to ask for more information if necessary.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
i ran the above program in jbuilder. it gives the answer ,Ba,B1. its correct
how did u get the second ans B2,Ba,B1 ..(reference sharanya)
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nadarajah,

After the execution of the method invocation expression m1(x) the object created in method m1 is eligible for garbage collection. The name of that instance is B2. If the garbage collector ran as soon as B2 became eligible for garbage collection, then the output of the program would be B2, Ba, B1.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic