• 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

Garbage Collection

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private static void m1(I[] a1) {a1 = null;}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) { //5
System.out.print(a1[i]);
}}}

After method m1 returns, the object created on which line is eligible for garbage collection?

a. 1
b. 2
c. 3
d. 4
e. Compile-time error
f. Run-time error
g. None of the above

This is a question from previous paper.
Q.When the control will return to m1 ,the array a1 will be null so at line 5 there should be a compile time error?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving this to the Beginners forum...
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deshdeep,

Consider pass by value versus pass by reference. Java is always ... which one? How does that apply here?

Also, even if the object is null, a) the compiler won't know that (it's a runtime condition) and b) it's a valid state. You'll only get an error (NullPointerException at runtime) if you try to access a field or method on a null object.

I hope that helps to clarify.

Jules
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic