Win a copy of Hands On Software Engineering with Python this week in the Jython/Python 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
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Garbage Collection Problem on Sybex Practice Exam 2  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I just finished taking the second online practice tests from Boyarsky's book, and I'm stuck on the following problem.

I copied this code on the compiler and there is no output. The answer explanation states  the code will print "becomes a butterfly" once, but the
book states that finalize may never called even when the garbage collection runs. So, I would think the answer is that it will print "becomes a butterfly zero times". So, I want to understand why the answer is that the code will print "becomes a butterfly" once. Thanks

 
Bartender
Posts: 1868
81
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java takes care of this calling the finalize method for object automatically and there is nothing you can do about that.
You can ask Java to run garbage collection, but the system is free to ignore your requests.

It's possible you have discovered a typo/error with the exam.
You may want to see if this has previously been reported on the support site for this found here https://www.selikoff.net/

Side note:
The finalize method is on it's way to being deprecated and not used with the newer versions of Java.
 
author
Posts: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It is a bit awkward, but line #6 does assume that the GC runs. So, I think, you have to answer the question as if the GC runs and completes on that line.

Henry
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!