public class Test{
public static void main(String[] args){
System.out.println(A.a());
}
public static Object a(){
int a = 0;
try{
a++;
return a;
}finally {
a = a + 2;
}
}
}
1
public class Test{
public static void main(String[] args){
System.out.println(a());
}
public static Object a(){
String s = "AAA";
try{
s += "a";
return s;
}finally {
s += "s";
}
}
}
AAAa
finally中是对返回值做赋值操作时,返回值和finally中的语句无关。对字符串的+操作在这里也算是赋值操作。
import java.util.ArrayList;
public class Test{
public static void main(String[] args){
System.out.println(a());
}
public static Object a(){
ArrayList<Object> ar = new ArrayList<>();
ar.add(0);
try{
ar.set(0,1);
return ar;
}finally {
ar.set(0,2);
}
}
[2]
除赋值操作外,返回值都是返回finally操作之后的。