final finally finalize的理论与部分实践

来源:互联网 发布:编程里面绝对值的表示 编辑:程序博客网 时间:2024/06/14 06:39

尴尬经常问到的3个东西 

 

经常会问到的基础问题,final和finally可能比较熟悉,finalize就不行了

一:final—修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承。因此一个类不能既被声明为 abstract的,又被声明为final的。将变量或方法声明为final,可以保证它们在使用中不被改变。被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。被声明为final的方法也同样只能使用,不能重载。

[例外情况是:final修饰引用类型的方法参数时,只能要求引用对象的地址不能变,但是引用对象的属性值是可以修改的。]

 
二:finally—再异常处理时提供 finally 块来执行任何清除操作。如果抛出一个异常,那么相匹配的 catch 子句就会执行,然后控制就会进入 finally 块(如果有的话)。

从下面测试只能说,finally会跟在它前面的return之后执行,如果前面没有return,则依次执行。

并且对于有返回值的方法,

1,如果前面有return,并且返回值是基本类型,如test1(),那么finally可以改变返回值但不作为返回结果。返回结果追溯到最后一次return前。

       如果前面没有return,并且返回值是基本类型,如test2(),按照顺序执行,finally在最终的return之前执行,所以可以改变返回值并作为返回结果。

2,如果前面有return,并且返回值是引用类型,如testHasReturn(),那么finally可以改变返回值并且可作为返回结果返回。

       如果前面没有return,并且返回值是引用类型,如testNoReturn(),按照顺序执行,finally在最终的return之前执行,所以可以改变返回值并作为返回结果返回,但是该值在最后一次return之前依然可以被改变。

 

public static void main(String[] args) throws InterruptedException{System.out.println("****************************");int i=test1();System.out.println("after test!"+i);System.out.println("----------------------------");i=test2();System.out.println("after test2!"+i);System.out.println("****************************");Car car=testHasReturn();System.out.println(car.getBrand());System.out.println("----------------------------");Car car2=testNoReturn();System.out.println(car2.getBrand());}public static int test1() throws InterruptedException {int j=0;try {int i=9/0;//throw new NullPointerException();} catch (Exception e) {j=j+1;System.out.println("Exception:"+j);return j;} finally {j=j+1;System.out.println("finally:"+j);}return j;}public static int test2() throws InterruptedException {int j=0;try {int i=9/0;//throw new NullPointerException();} catch (Exception e) {j=j+1;System.out.println("Exception:"+j);//return j;} finally {j=j+1;System.out.println("finally:"+j);}return j;}public static Car testHasReturn() throws InterruptedException {Car car=new Car();try {int i=9/0;//throw new NullPointerException();} catch (Exception e) {System.out.println("before e "+car.getBrand());car.setBrand("BenChi");System.out.println("after e "+car.getBrand());return car;} finally {System.out.println("before f "+car.getBrand());car.setBrand("BaoMa");System.out.println("after f "+car.getBrand());}return car;}public static Car testNoReturn() throws InterruptedException {Car car=new Car();try {int i=9/0;//throw new NullPointerException();} catch (Exception e) {System.out.println("before e "+car.getBrand());car.setBrand("BenChi");System.out.println("after e "+car.getBrand());//return car;} finally {System.out.println("before f "+car.getBrand());car.setBrand("BaoMa");System.out.println("after f "+car.getBrand());}car.setBrand("BaoMa2");return car;}输出内容如下:****************************Exception:1finally:2after test!1----------------------------Exception:1finally:2after test2!2****************************before e nullafter e BenChibefore f BenChiafter f BaoMaBaoMa----------------------------before e nullafter e BenChibefore f BenChiafter f BaoMaBaoMa2

可能测试还不够全面。。。再看吧。。。


三:finalize—方法名。Java 技术允许使用 finalize() 方法在垃圾收集器将对象从内存中清除出去之前做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。它是在 Object 类中定义的,因此所有的类都继承了它。子类覆盖 finalize() 方法以整理系统资源或者执行其他清理工作。finalize() 方法是在垃圾收集器删除对象之前对这个对象调用的。