java-try,return和finally相遇时的各种情况
今天碰到了这样一个问题;使用try,return和finally会碰到的各种情况
1,try中有return时,执行顺序;
2,try和finally中都有return时,执行顺序;
3,运算代码在finally中,try中return返回值时的执行顺序;
代码:
public class tryfinally {
static int i=0;
public static void main(String[] args) {
int value=getValue1();
System.out.println(value);
System.out.println(getValue());
System.out.println(getValue1());
System.out.println(getValue2());
System.out.println(getValue3());
}
@SuppressWarnings("finally")
public static boolean getValue(){
try{
return true;
}finally{
return false;
}
}
@SuppressWarnings("finally")
public static int getValue1(){
try{
return i;
}finally{
i++;
System.out.println(i);
}
}
@SuppressWarnings("finally")
public static int getValue2(){
int y=0;
try{
return y;
}finally{
y++;
System.out.println(y);
}
}
}
@SuppressWarnings("finally")
public static int getValue3(){
int z=0;
try{
return z;
}finally{
z++;
}
}
结果:false
1
0
1
0
1
0
0
我的结论:代码按顺序执行,有多个return时返回的同一个值时后者覆盖前者。
最新文章
- for循环与for in,$('').each 与$.each的区别
- 浅谈时钟的生成(js手写代码)
- LINQ系列:Linq to Object串联操作符
- sybase 收集常用sql语句
- [转载]C/C++可变参数之va_start和va_end使用详解
- 学习笔记:HSB、HSL
- 如何在JBoss WildFly 8 自定义log4j日志
- 简单几步让Chrome浏览器也能打开Oracle EBS
- redhad借用CentOs yum 安装
- IOS设置背景色设置最简单方法
- #Leet Code# Gray Code
- 【转】MFC中调试过程中查看输出信息 -- 不错
- XJOI网上同步训练DAY5 T1
- Python的TkinterButton做为父窗口
- 一个不可思议的MySQL慢查分析与解决
- Alpha冲刺Day11
- [LeetCode] Split Array with Equal Sum 分割数组成和相同的子数组
- phpstorm 不能自动打开上次的历史文件
- 06 自学Aruba之win7系统802.1x认证网卡设置指导
- MYSQL和oracle 大小写问题
热门文章
- Elastic Stack 笔记(十)Elasticsearch5.6 For Hadoop
- pip安装路径
- 09 (OC)* 键路径(keyPath)、键值编码(KVC)、键值观察(KVO)
- java反射机制,以及对反射机制的了解
- ASP.NET Core 3.0 : 二十五. TagHelper
- 5分钟了解Prometheus
- Postman 之 HTTP Multipart/form-data 调试
- Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利
- 通过父级id获取到其下所有子级(无穷级)——Mysql函数实现
- springboot结合mybatis使用pageHelper插件进行分页查询