当前位置:网站首页>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返回的值为准!
边栏推荐
- 【mysql学习08】
- Use three.js to realize the cool cyberpunk style 3D digital earth large screen
- JS中的对象
- 活动报名 | 玩转 Kubernetes 容器服务提高班正式开营!
- W5500通过上位机控制实现调节LED灯带的亮度
- 11. Reading rumors spread with deep learning
- W5500 multi node connection
- How to judge the performance of static code quality analysis tools? These five factors must be considered
- SQL injection LESS18 (header injection + error injection)
- Wiznet embedded Ethernet technology training open class (free!!!)
猜你喜欢

Information management system for typical works of urban sculpture (picture sharing system SSM)

Small and micro enterprise smart business card management applet

世界上最高效的笔记方法(改变你那老版的记笔记方法吧)

The most efficient note taking method in the world (change your old version of note taking method)

Talking about Devops monitoring, how does the team choose monitoring tools?

【mysql学习09】

Oil monkey script link

How to judge the performance of static code quality analysis tools? These five factors must be considered
![[leetcode brush questions]](/img/86/5f33a48f2164452bc1e14581b92d69.png)
[leetcode brush questions]

W5500在处于TCP_Server模式下,在交换机/路由器网络中无法ping通也无法通讯。
随机推荐
擎创科技加入龙蜥社区,共建智能运维平台新生态
Dynamic planning problem 03_ Maximum sub segment sum
菜单栏+状态栏+工具栏==PYQT5
Functions in JS
"Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities
[dynamic planning] 70. Climbing stairs
The most efficient note taking method in the world (change your old version of note taking method)
Small and micro enterprise smart business card management applet
Filter过滤器解决request请求参数乱码的原理解析
Oil monkey script link
Use three.js to realize the cool cyberpunk style 3D digital earth large screen
cookie and session
JS中的对象
使用Three.js实现炫酷的赛博朋克风格3D数字地球大屏
Job interviews are always a second kill? After reading the seckill system notes secretly stored by JD T8, I have given my knees
Flinksql client connection Kafka select * from table has no data error, how to solve it?
Web APIs (get element event basic operation element)
软件缺陷的管理
Activity registration | play with kubernetes container service improvement class officially opened!
Menu bar + status bar + toolbar ==pyqt5
