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

简介: 每日一道面试题之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 来代替。

相关文章
|
1月前
|
消息中间件 负载均衡 Kafka
【Kafka面试演练】那Kafka消费者手动提交、自动提交有什么区别?
嗯嗯Ok。分区的作用主要就是为了提高Kafka处理消息吞吐量。每一个topic会被分为多个分区。假如同一个topic下有n个分区、n个消费者,这样的话每个分区就会发送消息给对应的一个消费者,这样n个消费者负载均衡地处理消息。同时生产者会发送消息给不同分区,每个分区分给不同的brocker处理,让集群平坦压力,这样大大提高了Kafka的吞吐量。面试官思考中…
70 4
|
2月前
|
存储 安全 关系型数据库
|
1月前
|
编译器 C++ Python
【C/C++ 泡沫精选面试题02】深拷贝和浅拷贝之间的区别?
【C/C++ 泡沫精选面试题02】深拷贝和浅拷贝之间的区别?
33 1
|
2月前
|
存储 SQL 数据库
面试题20: 存储过程和函数的区别
面试题20: 存储过程和函数的区别
|
3月前
|
Java
【面试问题】Synchronized 和 ReentrantLock 区别?
【1月更文挑战第27天】【面试问题】Synchronized 和 ReentrantLock 区别?
|
3月前
|
Java 编译器 API
【面试问题】JDK 和 JRE 的区别?
【1月更文挑战第27天】【面试问题】JDK 和 JRE 的区别?
|
3月前
|
存储 JavaScript
面试官:请你说一说vuex的五个属性,分别是什么,区别和用途说一下(三)
面试官:请你说一说vuex的五个属性,分别是什么,区别和用途说一下
|
3月前
|
前端开发 JavaScript
面试官:请你说一说vuex的五个属性,分别是什么,区别和用途说一下(二)
面试官:请你说一说vuex的五个属性,分别是什么,区别和用途说一下
|
20天前
|
Java 关系型数据库 MySQL
大厂面试题详解:Java抽象类与接口的概念及区别
字节跳动大厂面试题详解:Java抽象类与接口的概念及区别
40 0
|
25天前
|
存储 JSON Java
面试官:Session和JWT有什么区别?
JSON Web Token (JWT) 是一种开放标准,用于安全地在网络上传输信息。JWT 包含头部、载荷和签名三部分,常用于身份验证和授权。与Session相比,JWT有以下优势:无服务器存储状态,支持跨域,适应微服务架构,自包含且可扩展。在Java开发中,可以使用HuTool框架操作JWT,包括生成、验证和解析Token。JWT通过在客户端存储令牌实现无状态认证,与Session的主要区别在于工作原理、存储方式和有效期管理。
32 6