版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42528266/article/details/103079098
/**
* @program: java_base
* @description:
* @author: ChenWenLong
* @create: 2019-11-14 16:46
**/
public class TestReturnAndFinally {
public static void main(String[] args) {
System.out.println(testReturn());// 1
System.out.println(testFinally());// 1
System.out.println(test());// 1
System.out.println(testAdd());// 2
}
public static int testReturn(){
int x = 1;
try {
return 1;
}finally {
x = 2;
}
}
public static int testFinally(){
int x = 1;
try {
return x;
}finally {
return 2;
}
}
public static int testAdd(){
int x = 1;
try {
return x;
}finally {
int y = ++x;
return y;
}
}
public static int test(){
int x = 1;
try {
return x;
}finally {
int y = x++;
return y;
}
}
}