当前位置:网站首页>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返回的值为准!
边栏推荐
猜你喜欢

Wiznet embedded Ethernet technology training open class (free!!!)

Eigenvalues and eigenvectors of matrices

软件缺陷的管理

第4章线性方程组

什么是全局事件总线?

Teach you how to configure S2E as the working mode of TCP server through MCU
![[leetcode brush questions]](/img/86/5f33a48f2164452bc1e14581b92d69.png)
[leetcode brush questions]

SQL injection less17 (error injection + subquery)

Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)

JVM性能调优方法
随机推荐
用 Redis 做一个可靠的延迟队列
模型部署简述
Game backpack system, "inventory Pro plug-in", research and learning ----- mom doesn't have to worry that I won't make a backpack anymore (unity3d)
JS中的数组
教你如何通过MCU配置S2E为TCP Client的工作模式
Common web attacks and defense
油猴脚本链接
2022 年中回顾|一文看懂预训练模型最新进展
JS operator
How does the whole network display IP ownership?
常见WEB攻击与防御
ArcMap cannot start the solution
JS数据类型以及相互转换
什么是全局事件总线?
Teach you how to configure S2E to UDP working mode through MCU
Fillet big killer, use filter to build fillet and wave effect!
W5500 multi node connection
Getting started with tensorflow
Web APIs (get element event basic operation element)
Plot ==pyqt5
