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;
}
转载于:https://www.cnblogs.com/withoutaword/p/8462771.html
最后
以上就是怡然跳跳糖最近收集整理的关于两个 integer 值判断是否相等的全部内容,更多相关两个内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复