在我下面的代码中,我检查了两个 infocode
如果它存在 show msg
MSG_SAME_INFO_ALREADY_EXISTS
但问题是当我比较两个 infocode
infocode值相同但不能进入循环。
我在下面的代码中错了什么?
if (folderInfoData.getFolderInforecord().getInfoCode() == map.get("infoCode")) {
showError(pageResourceBundle.getText("MSG_SAME_INFO_ALREADY_EXISTS"));
return;
}
下面给出的两个调试值:
folderInfoData.getFolderInforcord().getInfoCode()
和 map.get("infoCode")
信息代码返回下面给出的整数。
谁能告诉我如何解决这个问题?
谢谢
最佳答案
您正在比较对象 - Integer
s ,==
运算符可能仅适用于 [-128,127] 之间的数字。看JLS :
If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
由于您要比较的值不在上述范围内,因此结果将被评估为 false
。你应该使用 Integer#equals
相反。
关于java - java中两个整数值相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21088031/