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;
        }
  • 相关阅读:
    Mac 上所有的命令行相关问题的总结
    ThinkPHP中的统计查询方法
    织梦任意页面调用{dede:field.content/}的方法
    PHP isset()、empty()、is_null()的使用区别详解
    Dede更新提示DedeTag Engine Create File False的解决办法
    cookie 和session 的区别
    织梦后台如何设置手机站
    PHP时间戳和日期转换
    dede列表页调用
    dede图集内容页调用
  • 原文地址:https://www.cnblogs.com/miaoying/p/10845665.html
Copyright © 2011-2022 走看看