当前位置:网站首页>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的连接,那么该对象是可以被复活一次的,也就是该对象会从收集阶段重新回到应用阶段。
以上就是这三者的区别!!!
边栏推荐
- Comparison between FileInputStream and bufferedinputstream
- 初识指针笔记
- C language to achieve mine sweeping game (full version)
- 分支语句和循环语句
- C语言入门指南
- JS interview questions (I)
- [中国近代史] 第九章测验
- Inheritance and polymorphism (Part 2)
- First acquaintance with C language (Part 2)
- A brief introduction to the database of tyut Taiyuan University of technology in previous years
猜你喜欢
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
Relational algebra of tyut Taiyuan University of technology 2022 database
9. Pointer (upper)
1.C语言矩阵加减法
Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
Smart classroom solution and mobile teaching concept description
Wei Pai: the product is applauded, but why is the sales volume still frustrated
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
Application architecture of large live broadcast platform
随机推荐
凡人修仙学指针-2
最新坦克大战2022-全程开发笔记-1
What are the advantages of using SQL in Excel VBA
9.指针(上)
2-year experience summary, tell you how to do a good job in project management
A brief introduction to the database of tyut Taiyuan University of technology in previous years
(超详细二)onenet数据可视化详解,如何用截取数据流绘图
Arduino+ds18b20 temperature sensor (buzzer alarm) +lcd1602 display (IIC drive)
Inheritance and polymorphism (I)
System design learning (III) design Amazon's sales rank by category feature
Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology
TYUT太原理工大学2022数据库大题之概念模型设计
Application architecture of large live broadcast platform
2.C语言初阶练习题(2)
【九阳神功】2021复旦大学应用统计真题+解析
【九阳神功】2018复旦大学应用统计真题+解析
System design learning (I) design pastebin com (or Bit.ly)
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
Counter attack of flour dregs: redis series 52 questions, 30000 words + 80 pictures in detail.
西安电子科技大学22学年上学期《基础实验》试题及答案