Java try/catch/finally代码块中加入return后的执行顺序问题

本文探讨了Java中finally块内return语句的行为特征,包括如何影响引用类型与基础类型的返回值,以及这些行为背后的内存分配原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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要返回的值,因为这个值已经与变量无关了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值