关于Integer的比较,今天又犯了一个低级错误,记录下

今天查看以前所写的代码,看到有一部分被人改了,代码如下:


if
(orgId != organizationUpdateReq.getOrgId()) { //orgId的类型为Integer,organizationUpdateReq.getOrgId()的返回值也是Integer
throw ParameterAssert.warpExcp(ErrorCodeConstants.WRONG_PARAMETER_NOT_ALLOW, "错误!"); 

}

一个很明显的错误,当时我的脑海中是这样想的:

Integer会自动拆箱进行比较,这样就变成了以下的代码:

if (orgId.intValue() != organizationUpdateReq.getOrgId().intValue()) {  //orgId的类型为Integer,organizationUpdateReq.getOrgId()的返回值也是Integer
throw ParameterAssert.warpExcp(ErrorCodeConstants.WRONG_PARAMETER_NOT_ALLOW, "错误!"); 

}

看来有些还是搞混了,Integer的比较是对象之间的比较,不会进行自动拆箱,拆箱和装箱的时机是发生在赋值操作中。

引以为戒!!!!错的太low了。

原文地址:https://www.cnblogs.com/toSeeMyDream/p/8204500.html