当前位置:网站首页>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的连接,那么该对象是可以被复活一次的,也就是该对象会从收集阶段重新回到应用阶段。
以上就是这三者的区别!!!
边栏推荐
- Set container
- [中国近代史] 第九章测验
- 4.二分查找
- 5.MSDN的下载和使用
- View UI Plus 發布 1.3.1 版本,增强 TypeScript 使用體驗
- Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
- Data manipulation language (DML)
- The latest tank battle 2022 - full development notes-3
- Alibaba cloud microservices (III) sentinel open source flow control fuse degradation component
- Introduction pointer notes
猜你喜欢

3.输入和输出函数(printf、scanf、getchar和putchar)

Mortal immortal cultivation pointer-1

继承和多态(上)

TYUT太原理工大学2022数据库之关系代数小题

C语言入门指南

Alibaba cloud microservices (III) sentinel open source flow control fuse degradation component

View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件

9. Pointer (upper)

2.初识C语言(2)

TYUT太原理工大学2022数据库大题之数据库操作
随机推荐
Common method signatures and meanings of Iterable, collection and list
Quickly generate illustrations
Floating point comparison, CMP, tabulation ideas
string
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
System design learning (III) design Amazon's sales rank by category feature
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
图书管理系统小练习
Differences and application scenarios between MySQL index clock B-tree, b+tree and hash indexes
凡人修仙学指针-1
Inheritance and polymorphism (Part 2)
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
[中国近代史] 第五章测验
TYUT太原理工大学2022数据库大题之E-R图转关系模式
MySQL limit x, -1 doesn't work, -1 does not work, and an error is reported
3.猜数字游戏
String class
继承和多态(上)
【九阳神功】2018复旦大学应用统计真题+解析
学编程的八大电脑操作,总有一款你不会