1.final修飾類,方法,變量app
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 } }