前言
首先有如下代码:
package hpsyche.string;
/**
* @author Hpsyche
*/
public class MyTest {
static Integer count=10;
static Integer method(){
return count++;
}
public static void main(String[] args) {
System.out.println(method());
}
}
可以看到输出结果为10,结果合理,先return count,再++;
引入
再看如下例:
public class MyTest {
static Integer count=10;
static Integer method(){
try{
return count++;
}finally {
return count

本文通过实例代码分析并结合字节码解释了在`try-finally`结构中,return语句与finally块如何交互,特别是当finally中包含return时,如何处理返回值。在finally中有return的情况下,try块的返回值会被保存,finally中的修改不会影响已保存的返回值,导致了意外的结果。而如果没有return,finally块执行完毕后会返回try块的计算结果。
最低0.47元/天 解锁文章
29

被折叠的 条评论
为什么被折叠?



