例子如下,返回结果不同,前两个方法为finally没处理前的结果,而后一个方法的返回值为处理后的结果。请问这是为什么。
public class Test {
public static void main(String[] args) {
System.out.println(test1());
System.out.println(test2());
System.out.println(test3());
}
private static int test1()
{
int n=1;
try
{
return n;
}
finally
{
n=n+1;
}
}
private static String test2()
{
String x = new String();
x="xxx";
try
{
return x;
}
finally
{
x=x+"a";
}
}
private static StringBuffer test3()
{
StringBuffer b = new StringBuffer();
b.append("xxx");
try
{
return b;
}
finally
{
b.append("a");
}
}
}
拉莫斯之舞
相关分类