Java的基础知识整理

Java的基础知识整理

1、finally

  1. finally块一定会执行,无论是否try…catch
  2. finally前有return,会先执行return语句,并保存下来,再执行finally块,最后return
  3. finally前有return、finally块中也有return,先执行前面的return,保存下来,再执行finally的return,覆盖之前结果,并返回。
实践1
public class TestFinally {

    private int test(int a) {
        try {
            return  100/a;
        } catch (Exception e){
            System.out.println("catch");
        }finally {
            System.out.println("finally");
        }
        return -99;
    }

    public static void main(String[] args) {
        TestFinally obj = new TestFinally();
        System.out.println(obj.test(0));
    }
}

结果

catch
finally
-99

验证了第二条

实践2
public class TestFinally {

    private int test(int a) {
        try {
            return  100/a;
        } catch (Exception e){
            System.out.println("catch");
        }finally {
            System.out.println("finally");
            return -88;
        }
    }

    public static void main(String[] args) {
        TestFinally obj = new TestFinally();
        System.out.println(obj.test(0));
    }
}

结果

catch
finally
-88

验证了第三条

实践3
public class TestFinally {

    private int test(int a) {
        try {
            return  100/a;
        } catch (Exception e){
            System.out.println("catch");
            return -77;
        }finally {
            System.out.println("finally");
        }
    }

    public static void main(String[] args) {
        TestFinally obj = new TestFinally();
        System.out.println(obj.test(0));
    }
}

结果

catch
finally
-77

catch中的return会覆盖第一个return

实践4
public class TestFinally {

    private int test(int a) {
        try {
            return  100/a;
        } catch (Exception e){
            System.out.println("catch");
            return -77;
        }finally {
            System.out.println("finally");
            return -88;
        }
    }

    public static void main(String[] args) {
        TestFinally obj = new TestFinally();
        System.out.println(obj.test(0));
    }
}

结果

catch
finally
-88

finally中的return会覆盖前面的return

2、ArrayList

List<String> list = new ArrayList(); // list={}
list.add("a");       // list.size=10(默认大小10),扩容是size + size/2
ArrayList 与 Vector 区别呢?为什么要用Arraylist取代Vector呢?

Vector类的所有方法都是同步的。可以由两个线程安全地访问一个Vector对象、但是一个线程访问Vector的话代码要在同步操作上耗费大量的时间。

Arraylist不是同步的,所以在不需要保证线程安全时建议使用Arraylist

3、protected

Java中,可以使用访问控制符来保护对类、变量、方法和构造方法的访问。

  • default (即默认,什么也不写): 在同一包内可见,不使用任何修饰符。使用对象:类、接口、变量、方法。

  • private : 在同一类内可见。使用对象:变量、方法。 注意:不能修饰类(外部类)

  • protected : 对同一包内的类和所有子类可见。使用对象:变量、方法。 注意:不能修饰类(外部类)

  • public : 对所有类可见。使用对象:类、接口、变量、方法

示例:
// 1、创建 包1下的父类

public class ParentObj {

    int fun1() {   // default
        return 1;
    }

    protected int fun2() {   // protected
        return 2;
    }
}

// 2、同包下的子类,两个方法都能继承,能访问父类实例的default和protected方法

public class SonObj extends ParentObj {

    @Override
    int fun1() {   // default
       return super.fun1();
    }

    @Override
    protected int fun2() {   // protected
        return super.fun2();
    }
    public static void main(String[] args) {
     ParentObj parentObj = new ParentObj();
     parentObj.fun1();  // 可以访问父类实例default方法
     parentObj.fun2();  // 可以访问父类实例protected方法
 }
}

// 3、不同包下的子类,protected可以继承, default不能继承,不能访问父类实例的default和protected方法

public class SonObj2 extends ParentObj {

    @Override   // Method does not override method from its superclass
    int fun1() {   // default
       return super.fun1(); // java: fun1()在com.module.ParentObj中不是公共的; 无法从外部程序包中对其进行访问
    }

    @Override
    protected int fun2() {   // protected
        return super.fun2();
    }

    public static void main(String[] args) {
        ParentObj parentObj = new ParentObj();
        parentObj.fun1();  // java: fun1()在com.module.ParentObj中不是公共的; 无法从外部程序包中对其进行访问
        parentObj.fun2();  // java: fun2() 在 com.module.ParentObj 中是 protected 访问控制
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值