每日一道面试题之final、finally、finalize 有什么区别?

final:

final是Java中的关键字,用于修饰变量、方法或类,被final修饰的类表示该类不能被继承,被final修饰的变量表示该变量不能赋新的值,被final修饰的方法表示该方法不能被重写.

finally:

finally是Java中的关键字,用于定义在try-catch语句块中的一个代码块,无论是否发生异常都会执行finally代码块中的语句,常用于一些流的关闭。

finalize:

finalize是Java中Object类中的一个方法,用于在垃圾回收器回收对象之前执行一些清理操作,是一个对象的终结方法

如下所示为该方法的源码:

    protected void finalize() throws Throwable { }
}
/*Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.  A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. 
The general contract of finalize is that it is invoked if and when the Java™ virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized.  The finalize method may take any action, including making this object available again to other threads;  the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded.  For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded. 
The finalize method of class Object performs no special action;  it simply returns normally.  Subclasses of Object may override this definition. 
The Java programming language does not guarantee which thread will invoke the finalize method for any given object.  It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked.  If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates. 
After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded. 
The finalize method is never invoked more than once by a Java virtual machine for any given object. 
Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.*/'



/*当垃圾回收确定不再有对对象的引用时,由垃圾回收器对对象调用。子类覆盖finalize方法以处置系统资源或执行其他清理。

finalize的一般约定是,当Java虚拟机确定不再有任何方法可以让任何尚未死亡的线程访问该对象时,它将被调用,除非是由于其他一些准备结束的对象或类的结束所采取的操作。finalize方法可以采取任何动作,包括使该对象再次对其他线程可用;然而,finalize的通常目的是在不可撤销地丢弃对象之前执行清理操作。例如,表示输入/输出连接的对象的finalize方法可能会在对象被永久丢弃之前执行显式的I/O事务来中断连接。

类Object的finalize方法不执行任何特殊操作;它只是正常返回。Object的子类可以覆盖这个定义。

Java编程语言不保证哪个线程将为任何给定对象调用finalize方法。但是,可以保证调用finalize的线程在调用finalize时不会持有任何用户可见的同步锁。如果finalize方法抛出未捕获的异常,则忽略该异常并终止该对象的终结。

在finalize方法被对象调用之后,在Java虚拟机再次确定没有任何方法可以让任何尚未死亡的线程访问该对象之前,不会采取任何进一步的操作,包括准备结束的其他对象或类的可能操作,此时该对象可能被丢弃。

Java虚拟机对任何给定对象调用finalize方法的次数永远不会超过一次。

finalize方法抛出的任何异常都会导致此对象的终结终止,但会被忽略。*/

简单点说:finalize方法一般情况下并不需要我们去实现,而是在对象被 GC 回收之前通过由垃圾回收器对对象调用当垃圾回收器确定不再有对对象的引用时,垃圾回收器会在某个时间回收该对象,并在回收之前调用该对象的 finalize() 方法,在 finalize() 方法中,可以对该对象进行一些清理工作,如释放一些资源等,但finalize的通常目的是在不可撤销地丢弃对象之前执行清理操作。例如,表示输入/输出连接的对象的finalize方法可能会在对象被永久丢弃之前执行显式的I/O事务来中断连接,但是应该注意的是,finalize() 方法的执行时间是不确定的,甚至有可能不执行。因此,不应该在 finalize() 方法中依赖于执行的顺序或时间

另外,在 Java 9 及其之后的版本中,finalize() 方法已被废弃,建议使用 Cleaner API 来代替。

你可能感兴趣的:(Java八股文面试题,java,jvm,面试,后端,职场和发展)