当前位置:网站首页>fianl、finally、finalize三者的区别
fianl、finally、finalize三者的区别
2022-07-06 09:20:00 【快醒醒鸭今天你编程了吗?】
首先这三者其实是没有本质关系的,面试的时候会考察我们对这块的理解:
fianl
fianl是用来修饰变量,修饰类,修饰类属性,当它修饰变量的时候,变量的引用不可变但是地址中的内容可以改变,当它修饰类的时候,这个类不可被继承但是可以被重载,当它修饰类属性的时候,子类就不能给这个属性重新赋值,如果赋值会报错。
finally
常用语try catch finally中,在异常处理时用finally执行任何清除操作,不管有没有异常抛出和捕获finally中的语句都会执行
如果try代码块里面有return语句,return语句还一一定要是最后执行的,java中有一条亘古不变的规则,程序是自上而下逐行执行,一旦执行了return整个方法结束。
咱们看一道题目:
package com.ws;
public class Demo02 {
public static void main(String[] args) {
int result = m();
System.out.println(result);
}
public static int m(){
int i = 100;
try{
return i;
}finally {
i++;
}
}
}
以上输出结果是什么?我猜大多数人肯定会说是101,但是答案是100
咱们来看看反编译之后的代码
//以下就是反编译之后的代码
public static int m(){
int i = 100;
int j = i;
i++;
return j;
Exception exception;
exception;
i++;
throw exception;
}
首先是把i的值给j,然后i++(就是执行fianlly里面的代码),然后返回j。
finalize
在垃圾回收中,如果被回收的对象重写了finalize()方法,并且在finalize()方法中重新建立了该对象与GC ROOT的连接,那么该对象是可以被复活一次的,也就是该对象会从收集阶段重新回到应用阶段。
以上就是这三者的区别!!!
边栏推荐
- JS interview questions (I)
- TYUT太原理工大学2022软工导论考试题型大纲
- One article to get UDP and TCP high-frequency interview questions!
- Branch and loop statements
- 3.C语言用代数余子式计算行列式
- Database operation of tyut Taiyuan University of technology 2022 database
- Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
- View UI plus released version 1.3.0, adding space and $imagepreview components
- 3. C language uses algebraic cofactor to calculate determinant
- FileInputStream和BufferedInputStream的比较
猜你喜欢
5. Download and use of MSDN
Smart classroom solution and mobile teaching concept description
Design a key value cache to save the results of the most recent Web server queries
凡人修仙学指针-2
最新坦克大战2022-全程开发笔记-2
Quickly generate illustrations
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
IPv6 experiment
9. Pointer (upper)
凡人修仙学指针-1
随机推荐
2.C语言初阶练习题(2)
View UI plus released version 1.2.0 and added image, skeleton and typography components
String class
魏牌:产品叫好声一片,但为何销量还是受挫
更改VS主题及设置背景图片
Solution: warning:tensorflow:gradients do not exist for variables ['deny_1/kernel:0', 'deny_1/bias:0',
Questions and answers of "Fundamentals of RF circuits" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
【九阳神功】2020复旦大学应用统计真题+解析
凡人修仙学指针-2
TYUT太原理工大学2022数据库大题之E-R图转关系模式
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)
2. C language matrix multiplication
Database operation of tyut Taiyuan University of technology 2022 database
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
Abstract classes and interfaces
Atomic and nonatomic
arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)
Arduino+ water level sensor +led display + buzzer alarm
6.函数的递归
Small exercise of library management system