java - java中两个整数值相等?

标签 java dictionary integer equals record

在我下面的代码中,我检查了两个 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") 信息代码返回下面给出的整数。

enter image description here enter image description here

谁能告诉我如何解决这个问题?

谢谢

最佳答案

您正在比较对象 - Integers== 运算符可能仅适用于 [-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/

相关文章:

python - 通过 Python 中的集合成员资格对整数进行分组

链接到批处理文件的java变量

java - 从 java 代码中运行已编译的 java .class 文件并捕获输出

python - 获取两个字典列表之间的差异(增量)

python - 自定义字典的Python字典更新

c# - 将连续的 64 位数字统一哈希为 8 位?

java - 按降序对基本类型数组进行排序

java - 当任务出现错误时,不要开始下一个任务并停止流程

c# - 如何传递正确的 Model Diction 类型 MVC

c - 显式 int32 -> float32 转换的规则