Pergunta de entrevista da empresa Bloomberg

I've been asked three questions. The only one I missed is quite a basic question:If you want to do something before Java GC recycle the object?

Respostas da entrevista

Sigiloso

26 de mar. de 2014

Just simply override the Finalize() function.

1

Sigiloso

13 de fev. de 2015

Not quite. Finalize() is unreliable, in some cases it is not called (e.g. when exiting the application and shutting down the runtime). If you want to call something every time reliably (aka destructor in C++) use the reliable try/finally block. The question does not say that it needs to be run "right before GC", just "before GC". And because you cannot call any method after GC (other than fiddling with Unsafe), everything you call at any time during the program execution is called "before GC".

1

Sigiloso

8 de set. de 2014

override finalize() method. This is the method called right before GC recycle the object