1.final修饰类,方法,变量
public class Student { public final StringBuffer str = new StringBuffer("old"); public static void main(String[] args) { Student stu = new Student(); stu.str.append(" man"); //地址没有变,但是内容改变了 System.out.println(stu.str);// old man } }
2.finally
public class Finalll { public static void test(){ try { System.out.println("try block"); System.exit(0);//终止JVM } finally { System.out.println("finally block"); } } public static void main(String[] args) { test();//try block } }
public class Finalll { public static int test(){ int a = 1; try { System.out.println("try block"); return 1; } finally { System.out.println("finally block"); //这里并没有使用return,所以只是更新了a的值,并没有更新临时栈的值, 所以返回值还是1 a = 2; } } public static void main(String[] args) { System.out.println(test()); //输出结果: try block finally block 1 } }