zoukankan      html  css  js  c++  java
  • 多catch块的代码优化

    一、多catch块的代码优化

    在写代码时,多行存在不同的异常,使用try catch的话,习惯性的是有多个catch,如下所示:

    注意到warning,文字描述如下:

    Reports identical catch sections in try blocks under JDK 7. A quickfix is available to collapse the sections into a multi-catch section.
    This inspection only reports if the project or module is configured to use a language level of 7.0 or higher.

     大概意思是再try的代码块中,存在多个catch块结构时,如果使用的是JDK 7及以上,把这些catch块进行折叠到一个中更高效,如下所示:

    二、&&、&、|、||的区别

    另外,扩展一下&&、&、|、||的区别:简单来说就是&&,||有短路功能,而&、|没有,所以&&、||的性能更佳,测试代码如下:

        @Test
        public void test() {
            int c = 2;
            if (c == b() | c == a()) {
                // 会打印
                // b() 返回值: 2
                // a() 返回值: 1
                System.out.println("|  c = " + c);
            }
            System.out.println("|---- test end");
    
    
            if (c == b() || c == a()) {
                // 会打印
                // b() 返回值: 2
                System.out.println("||  c = " + c);
            }
            System.out.println("||---- test end");
    
    
            if (c == a() & c == b()) {
                // 会打印
                // a() 返回值: 1
                // b() 返回值: 2
                System.out.println("&  c = " + c);
            }
            System.out.println("&---- test end");
    
    
            if (c == a() && c == b()) {
                // 会打印
                // a() 返回值: 1
                System.out.println("&&  c = " + c);
            }
            System.out.println("&&---- test end");
    
        }
    
        public int a() {
            System.out.println("a() 返回值: " + 1);
            return 1;
        }
    
        public int b() {
            System.out.println("b() 返回值: " + 2);
            return 2;
        }
  • 相关阅读:
    OFBiz中services调用机制
    OFBiz中JOB的运行机制
    ofbiz中运行配置及流程
    ofbiz框架学习
    在java学习中的思考
    在更新操作中所需要注意的事项
    级联删除
    关于整合hibernate4和spring3的相关注意事项
    常见系统中文字体的英文名
    flash Socket 连接 以及 跨域策略文件crossdomain.xml配置详解
  • 原文地址:https://www.cnblogs.com/miaoying/p/10845665.html
Copyright © 2011-2022 走看看