首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

务必知道,别滥用-finally

2012-12-28 
必须知道,别滥用-finallypublic class Test {public static int testInt() {int x 0try {x 1System.

必须知道,别滥用-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

热点排行