经典笔试题-try{}里有一个return语句,那么紧跟在这个try后的finally{}里德code会不会被执行?什么时候被执行,在return之前还是

阅读 61

2022-03-11


代码示例
/**
* @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;
}
}
}



精彩评论(0)

0 0 举报