当前位置:网站首页>return 和 finally的执行顺序 ?各位大佬请看过来,
return 和 finally的执行顺序 ?各位大佬请看过来,
2022-07-25 11:00:00 【咻的一下飞过去】
思考题1 :
return 和 finally的执行顺序 ?
答: return 先执行 将方法停下,后执行finally ,最后 在执行 return将值返回
例如:
public class Return_Finally {
public static void main(String[] args) {
System.out.println(A.a());
}
}
class A {
public static int a() {
int i = 1;
try{
return i;
}finally {
System.out.println("f1==>>"+i);
++i;
System.out.println("f2==>>"+i);
}
}
}
结果:
可以看见在finally中已经改变了 i 的值最后还是返回了1,这就是思考题二要讨论的了:
思考题2 :
finally执行,然后return将值返回,如果在finally中将值改变了,那么retrun 后的值会不会变?
答:分情况 : retrun返回的值的数据类型
基本数据类型
return返回基本数据类型,且finally中没有return,只是将变量值改变, 这种情况
finally中不影响最终return的值
引用数据类型
return返回引用数据类型(除了String),且finally中没有return,只是将引用类型的值改变, 这种情况
finally中会影响最终return的值
如果finally中有retrun的话,就以finally中return返回的值为准!
边栏推荐
- Common web attacks and defense
- 数据库完整性——六大约束学习
- 油猴脚本链接
- Various controls ==pyqt5
- Breadth first traversal (problems related to sequence traversal of graphs and binary trees)
- 【IMX6ULL笔记】--内核底层驱动初步探究
- 第4章线性方程组
- Esp8266 uses drv8833 drive board to drive N20 motor
- Dynamic planning question 05_ Missile interception
- Attendance system based on w5500
猜你喜欢

工作面试总遇秒杀?看了京东T8大咖私藏的秒杀系统笔记,已献出膝盖

Leetcode sword finger offer 27. image of binary tree

JS数据类型以及相互转换

OneNET平台控制W5500开发板LED灯

JVM性能调优方法

JVM performance tuning methods

圆角大杀器,使用滤镜构建圆角及波浪效果!

如何判断静态代码质量分析工具的性能?这五大因素必须考虑

Activity registration | play with kubernetes container service improvement class officially opened!

SQL language (III)
随机推荐
谣言检测文献阅读十一—Preventing rumor spread with deep learning
Understanding: idea uses Scala to write wordcount programs and generate jar packages
大话DevOps监控,团队如何选择监控工具?
基于cornerstone.js的dicom医学影像查看浏览功能
Teach you how to configure S2E as the working mode of TCP client through MCU
JaveScript循环
RedisUtil
Various controls ==pyqt5
Maskgae: masked graph modeling meets graph autoencoders
WIZnet嵌入式以太网技术培训公开课(免费!!!)
SQL language (I)
Information management system for typical works of urban sculpture (picture sharing system SSM)
JS常用内置对象 数据类型的分类 传参 堆栈
Common linear modulation methods based on MATLAB
[MySQL learning 08]
JS process control
教你如何通过MCU将S2E配置为UDP的工作模式
How to judge the performance of static code quality analysis tools? These five factors must be considered
[electronic device notes 5] diode parameters and selection
leetcode 剑指 Offer 28. 对称的二叉树
