1、d引用不变,finally块中修改了对象的内容,返回d引用
public static Dog other() {
Dog d = new Dog();
try {
d.setName("aaa");
return d;
} catch (Exception e) {
} finally {
d.setName("bbbb");
}
return d;
}
bbbb
2、finally块中,d引用指向新对象,try中返回原对象的值
public static Dog other() {
Dog d = new Dog();
try {
d.setName("aaa");
return d;
} catch (Exception e) {
} finally {
d=new Dog();
d.setName("bbbb");
}
return d;
}
aaa
3、Integer 或String为final 类,值不可更改,finally 块中i重新赋值后,引用指向了2对象,不影响try中原对象1的返回
public static Integer other2() {
Integer i = 1;
try {
return i;
} catch (Exception e) {
} finally {
i = 2;
}
return i;
}
1
4、finally 块中i重新赋值后,引用指向了2对象,finally里的return语句则覆盖try中的return语句直接返回。
public static Integer other2() {
Integer i = 1;
try {
return i;
} catch (Exception e) {
} finally {
i = 2;
return i;
}
}
2