Integer比较大小所遇到的奇葩问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_18730505/article/details/80936624

this is code:

public boolean isMonthTable() {
   return (null == tableType) ? false : tableType == Constant.TableType.MONTH;
}

在 tomcat 下 跟踪代码 发现 凡是 调用 isMonthTable()

0==0  是 正确的

而在webserphere 中 远程调试 发现

0==0 是错误的

tableType 的类型 是 Integer

常量 Month 也是 Integer

这并不是基本数据类型。

==比较的是 对象引用的地址。

好比 

Integer a = 1

Integer b = 1

a==b 在 tomcat 下返回 true 

a==b 在websphere 下 返回false

如果仅限于 地址的比较  为啥 不同中间件 值是不一样的。。这是非常好奇的地方

不过 对象 的比较 最好 使用 equals  

或者  a.intValue()==b.intValue();


猜你喜欢

转载自blog.csdn.net/qq_18730505/article/details/80936624