java基础补充

1. try catch finally 中的return

public class testfinally {
    @Test
    public void test1(){
        System.out.println(theone());//3
        System.out.println(thetwo());//0
        System.out.println(thethree());//3 0
    }
    public static int theone(){
        int i = 0;
        try {
            return i;
        } catch (Exception e){
            return i+1;
        } finally {
            return 3;
        }
    }
    public static int thetwo(){
        int i = 0;
        try {
            return i;
        } catch (Exception e){
            return i+1;
        } finally {
            i = 3;
        }
    }
    public static int thethree(){
        int i = 0;
        try {
            return i;
        } catch (Exception e){
            return i+1;
        } finally {
            System.out.println(3);
        }
    }
}

知识点:
try中return后不会直接结束,而是会先把这个返回值挂起,然后去执行finally语句,如果finally语句中有return,还会把这个挂起值覆盖,最后返回给调用者的是这个挂起值.

2. String类型普通创建和常量池创建

public class TestString {
    @Test
    public void test1(){
        String a = "aaa";
        String b = "bbb";
        String c = "aaabbb";
        String d = "aaa"+"bbb";
        String e = a+b;
        System.out.println(c==d);//true
        System.out.println(c==e);//false
        String f = e.intern();
        System.out.println(c==f);//true
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值