【发布时间】:2013-12-21 11:39:56
【问题描述】:
代码:
call.getUserId().equals(ITConstants.SPECIALID)
public static final Integer SPECIALID= 0;
POJO:
public class ImCall implements java.io.Serializable {
private Integer userId;
HBM:
<property name="userId">
<column name="USER_ID" />
</property>
MySQL:
int(11) is the datatype in MySQL
当 UserId 为零时,“.equals()”不起作用(返回 false),但令人惊讶的是,“==”起作用了(返回 true)。 我以为是Tomcat服务器出了问题,所以我清理了它并重新启动。 还是同样的问题。
但又过了几天,我上面提到的问题没有再次出现。 但是现在,== 和 .equals() 都有效!
所以,我的第一个问题是,是否存在“==”有效而“.equals()”无效的情况。 第二个问题是,为什么 "==" 在这种情况下返回 true?
编辑:
Getter 只返回整数。
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
【问题讨论】:
-
显示你的价值观是什么以及预期的输出是什么。例如,我不能 100% 确定
call.getUserId()返回一个Integer,它可能返回一个int。 -
@skiwi Getters 仅返回整数。请查看我的编辑
标签: java mysql hibernate enums equals