读书人

务必知道别滥用-finally

发布时间: 2012-12-28 10:29:05 作者: rapoo

必须知道,别滥用-finally

public class Test {        public static int testInt() {        int x = 0;        try {            x = 1;            System.out.println("x=1");            return x;        }        finally {            x = 2;            System.out.println("x=2");        }    }        public static Ref testRef() {        Ref ref = new Ref();        try {            ref.x = 1;            System.out.println("x=1");            return ref;        }        finally {            ref.x = 2;            System.out.println("x=2");        }    }        public static void main(String[] args) {        System.out.println("x=? " + testInt());        System.out.println("x=? " + testRef());    }        private static class Ref {        private int x;        public String toString(){            return String.valueOf(x);        }    }}
1 楼 trydofor 2011-04-22 后续跟踪:
使用javap分析finally块中return值
http://blog.csdn.net/aspire_sun/archive/2011/04/22/6340532.aspx 2 楼 trydofor 2011-04-22 参考资料
http://supermmx.org/blog/20061109_try_catch_finally

读书人网 >编程

热点推荐