finally可以分两方面理解
1.执行时机问题。finally总会执行(除非是System.exit()),正常情况下在try后执行,抛异常时在catch后面执行
2.返回值问题。可以认为try(或者catch)中的return语句的返回值放入线程栈的顶部:如果返回值是基本类型则顶部存放的就是值,如果返回值是引用类型,则顶部存放的是引用。finally中的return语句可以修改引用所对应的对象,无法修改基本类型。但不管是基本类型还是引用类型,都可以被finally返回的“具体值”具体值覆盖
3.不建议在finally中使用return语句,如果有,eclipse会warning“finally block does not complete normally”
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package
com.ljn. base ; public class FinallyTest
{ public static void main(String[]
args) { System. out .println(test()); System. out .println(testPrimitive()); } public static Student
test() { Student
result = new Student( "Tom" ,
0); try { result.setAge(1); return result; } catch (Exception
e) { result.setAge(2); return result; } finally { result.setAge(3); //引用类型的返回值,可被修改 //return
new Student("Kobe", 33); //可以被“具体值”覆盖 } } public static int testPrimitive()
{ int x
= 0; try { x
= 1; return x; } catch (Exception
e) { x
= 2; return x; } finally { x
= 3; //基本类型的返回值,不可被修改 //return
x; //可以被“具体值”覆盖 } } private static class Student
{ private String
name; private int age; public Student(String
name, int age)
{ this .name
= name; this .age
= age; } @Override public String
toString() { return "Student
[name=" +
name + ",
age=" +
age + "]" ; } public void setAge( int age)
{ this .age
= age; } } }
返回基础类型变量时是值,返回引用类型时是指向某个对象的地址;
而且基础类型是被分配在栈中的,对象是被分配在堆中的,只要有引用指向这个对象,系统就不会回收此对象,
所以可以在后面的finally块中改变引用指向的对象的内容,却无法改变try语句中return要返回的值,因为这个值已经与变量无关了。
|