zoukankan      html  css  js  c++  java
  • 两个 integer 值判断是否相等

    1.如果比较Integer A a; Integer B b; 我们通常如下比较:

     
    if (null != a && null != b) {
        if(a.intValue() == b.intValue()) {
            // TODO
        }
    }
    

    2.有没有工具类,可以直接比较两个Integer的值的大小的?而不是上面这种臃肿的代码。

    补充:java中,用最简单的代码实现比较两个Integer的值是否相等,有什么好的实现方式?

    答:

    Integer num1 = 259; Integer num2 = null; System.out.println(org.apache.commons.lang.ObjectUtils.equals(num1, num2));

    上述方法如果num1和num2都为null时,比较的结果为真。如果定义为比较结果为假,那还不知道是否有开源的第三方工具方法支持,或者说自己写一个方法就好了。在java中,null==null表达式的结果也是为真。

    自定义方法:

     
      public static boolean compare(Integer num1,Integer num2){
            if(null == num1 || null == num2){
                return false;
            }
            
            return num1.equals(num2);
        }

    或者

    if (index == null && other.index != null) {
    return false;
    } else if (index != null && !index.equals(other.index)) {
    return false;
    }

    项目fix bug
    if(one.getOrderState() != OrderStateConfig.OrderPurchase) {
    return Boolean.FALSE;
    }
    改为
    if(OrderStateConfig.OrderPurchase.equals(one.getOrderState())) {
    return Boolean.FALSE;
    }




  • 相关阅读:
    java打包与热部署 爱上
    分组后最新的记录 爱上
    Js操作Excel常用方法 GO
    查找父元素和子元素 GO
    DataView不能按中文排序问题解决 GO
    浮动层居中的对话框效果演示 GO
    ajax form提交 GO
    SQL Server智能提示插件下载
    提高代码质量的三要素
    Div的宽度与高度设定100%
  • 原文地址:https://www.cnblogs.com/withoutaword/p/8462771.html
Copyright © 2011-2022 走看看