Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Garbage Collection

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Answer is : a,c,d,e

I can guess the reason for 'a' only. Wonder How come c,d,e, are there?
Anyone, please explain this.
[ October 27, 2004: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont see the answers!!! Can you the answers (options) to select...
 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The creation of a0 seems to be missing. do i assume a0 is also an instance of A, since a0= a3.
 
Vidyavathi saravanan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam sorry for the incomplete question.

Here it is

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

a. A1A2A3A1
b. A0A0A0A0A1A2A3
c. A1A2A3A1A2A3
d. A1A2A3A1A1A2A3
e. A1A2A3A1A3A2A1
f. A0A1A2A3A1A2A3

Answer is : a,c,d,e. Please explain this. I can guess 'a'. But wonder about the rest.?!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I always try is to compile and see the result myself.
I then insert 'printing'messages to try and trace the path that is being followed. However, in this case after compiling succesfully and trying to run it, I got this errormessage:
Exception in thread "main" java.lang.NoSuchMethodError: main

Anyone got a clue?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give class B the public modifier and put it in its own file B.java.
 
Eric Zanders
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry, it now results in answer d...
[ October 27, 2004: Message edited by: Eric Zanders ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The key point to this question is the call to System.gc(). When you call this method, you are asking for the JVM to run Garbage Collector. It will search for objects eligible for garbaging, and will call the finalize() method once in these objects. Note, however, that you can only suggest that the garbage collector should run, the JVM can completely ignore you, or can do just part of the work, there is no way for you to know.
So back to the question, waht happens is that after the call to m1(), we will have A1A2A3A4 printed in the console, as you would have expected, and three objets eligible for garbage colleting, pointed by a1, a2 and a3 (note that a0 is not an object, just a reference to one). So the Garbage Collector will finalize none, any or all of these objects, calling its finalize()'s methods, which prints their names. Therefore, the answers a,c,d,e are all possible to happen. Running the example in my computer I always got answer d, A1A2A3A1A1A2A3, but this doesn't mean that the others are not possible.
Hope this can be helpfull! Cheers!
 
Eric Zanders
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found it!!!

In a nutshell: you refer a1 to an A with value "A1" and an otherA with value "A2". Same goes for a2 (A2 & A3) and a3(A3 & A1).
In the for-loop you ask of a0 to refer to the otherA value of the A it is referring to, thus creating a loop. If you increase the maxvalue of i in the for-statement (ie to 9), you'll see A1A2A3A1A2A3A1A2A3A1A2A3, the last A1A2A3-cycle always being created by finalize() (due to the garbage collection).

Am I right? It may be that I still mix-up terminologies, for that I apologize. (This is only my 7� week of Java...)
[ October 27, 2004: Message edited by: Eric Zanders ]
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic