日期:2014-05-20  浏览次数:20657 次

Java中,关于return关键字的实行原理有所疑问
今天无聊,翻翻看了下网上流行的Java面试题,发现有这么一题。说“try中包含return,finally会不会执行?如果可以,什么时候执行”,答案给的是会执行,在try块中的return前面执行,小的好事,就试了一下,使用Eclipse debug,发现finally块中的代码确实会执行,但是是再try块return语句的后面。由此引起小猫的好奇,写了下面一段代码做了个测试:
public class HelloWorld{
public static void main(String args[]){

HelloWorld hw = new HelloWorld();
System.out.println(hw.get());
}
public String get() {
String a = "";
try{
a = "1";
System.out.println("1.a = " +a );
return a;
}catch(Exception e){
System.out.println("2.a = " +a );
}finally{
a = "2";
System.out.println("3.a = " +a );
return a;
}
}
}
输出结果是:
1.a = 1
3.a = 2
2
debug过程中发现两次return都有执行过并且方法的最后返回值等于最后一个return返回的。由此引出一个想法:当Java代码执行方法的时候,会在jvm中生成一个方法返回类型的一个实例,当Java代码执行时如果遇到return语句时候,会将return后面对象的实例复制给前面所说的方法生成的实例。请各位大大帮忙确认一下,小弟的想法是否正确。


------解决方案--------------------
楼主的理解是正确的..

在James Gosling的书里面有说明过这个问题.

摘录section 12.4节:

A finally clause is always entered with a reason. That reason may be that the try code finished normally, that it executed a control flow statement such as return, or that an exception was thrown in code executed in the TRy block. The reason is remembered when the finally clause exits by falling out the bottom. However, if the finally block creates its own reason to leave by executing a control flow statement (such as break or return) or by throwing an exception, that reason supersedes the original one, and the original reason is forgotten. For example, consider the following code:

try {
// ... do something ...
return 1;
} finally {
return 2;
}



When the TRy block executes its return, the finally block is entered with the "reason" of returning the value 1. However, inside the finally block the value 2 is returned, so the initial intention is forgotten. In fact, if any of the other code in the try block had thrown an exception, the result would still be to return 2. If the finally block did not return a value but simply fell out the bottom, the "return the value 1" reason would be remembered and carried out.

============

大意就是:
try内的代码正常执行完,就会带着"返回值是1"的原因进入finally
1)由于finally里面有return 2,所以之前的"返回值是1"则被遗忘,然就就返回2;
如果抛出异常了,返回值仍然是2.
2)如果finally块里面没有return,那么"返回值是1"则能被"记住"并且返回该值。


------解决方案--------------------
我猜测的是这样的,就像以前用 C 之类的写程序一样,返回值是压在堆栈的栈顶的。

所以,如果你 try + return + finally + return ,栈顶的元素被 finally 里面的东西覆盖了。
所以最后就返回了 finally 里面的东西,因为这时候一个方法完成后取它的返回值也是从栈顶上取第一个元素。