为什么在java中比较整数 Package 器时128==128为false而127==127为true?

qgzx9mmu  于 2021-07-06  发布在  Java
关注(0)|答案(7)|浏览(323)
class D {
    public static void main(String args[]) {
        Integer b2=128;
        Integer b3=128;
        System.out.println(b2==b3);
    }
}

输出:

false
class D {
    public static void main(String args[]) {
        Integer b2=127;
        Integer b3=127;
        System.out.println(b2==b3);
    }
}

输出:

true

注:介于-128和127之间的数字为真。

camsedfj

camsedfj1#

我写了以下内容,因为这个问题不仅仅是整数的问题。我的结论是,如果不正确地使用api,通常会看到不正确的行为。正确使用它,你应该看到正确的行为:

public static void main (String[] args) {
    Byte b1=127;
    Byte b2=127;

    Short s1=127; //incorrect should use Byte
    Short s2=127; //incorrect should use Byte
    Short s3=128;
    Short s4=128;

    Integer i1=127; //incorrect should use Byte
    Integer i2=127; //incorrect should use Byte
    Integer i3=128;
    Integer i4=128;

    Integer i5=32767; //incorrect should use Short
    Integer i6=32767; //incorrect should use Short

    Long l1=127L;           //incorrect should use Byte
    Long l2=127L;           //incorrect should use Byte
    Long l3=13267L;         //incorrect should use Short
    Long l4=32767L;         //incorrect should use Short
    Long l5=2147483647L;    //incorrect should use Integer 
    Long l6=2147483647L;    //incorrect should use Integer
    Long l7=2147483648L;
    Long l8=2147483648L;

    System.out.print(b1==b2); //true  (incorrect) Used API correctly
    System.out.print(s1==s2); //true  (incorrect) Used API incorrectly
    System.out.print(i1==i2); //true  (incorrect) Used API incorrectly
    System.out.print(l1==l2); //true  (incorrect) Used API incorrectly

    System.out.print(s3==s4); //false (correct) Used API correctly
    System.out.print(i3==i4); //false (correct) Used API correctly
    System.out.print(i5==i6); //false (correct) Used API correctly
    System.out.print(l3==l4); //false (correct) Used API correctly
    System.out.print(l7==l8); //false (correct) Used API correctly
    System.out.print(l5==l6); //false (correct) Used API incorrectly

}
i2loujxw

i2loujxw2#

其他的答案描述了为什么可以观察到观察到的效果,但是对于程序员来说,这确实是离题的(当然很有趣,但是在编写实际代码时应该完全忘记这一点)。
要比较整数对象是否相等,请使用 equals 方法。
不要尝试使用identity运算符比较整数对象的相等性, == .
有些相等的值可能是相同的对象,但这不是通常应该依赖的。

cclgggtu

cclgggtu3#

在这两种情况下,使用原语数据类型int都会产生true,即预期的输出。
但是,由于使用的是整数对象,==运算符的含义不同。
在对象的上下文中,==检查变量是否引用相同的对象引用。
要比较对象的值,应该使用equals()方法。

b2.equals(b1)

它将指示b2是小于b1、大于还是等于(有关详细信息,请查看api)

3zwtqj6y

3zwtqj6y4#

自动装箱缓存-128到127。这在jls(5.1.7)中有规定。
如果装箱的值p为true、false、一个字节、一个介于\u0000到\u007f之间的字符或一个介于-128到127之间的int或short数字,则让r1和r2为p的任意两个装箱转换的结果。通常情况下r1==r2。
处理对象时要记住的一个简单规则是-使用 .equals 如果要检查两个对象是否“相等”,请使用 == 当你想看看他们是否指向同一个示例。

moiiocjp

moiiocjp5#

在java中编译一个数字文本并将其赋给一个整数(大写 I )编译器发出:

Integer b2 =Integer.valueOf(127)

使用自动装箱时也会生成这行代码。 valueOf 实现时,某些数字被“池化”,对于小于128的值,它返回相同的示例。
java 1.6源代码第621行:

public static Integer valueOf(int i) {
    if(i >= -128 && i <= IntegerCache.high)
        return IntegerCache.cache[i + 128];
    else
        return new Integer(i);
}

价值 high 可以使用system属性配置为另一个值。
-djava.lang.integer.integercache.high=999
如果用该系统属性运行程序,它将输出true!
显而易见的结论是:永远不要依赖于两个参考文献是相同的,总是比较它们 .equals() 方法。
所以呢 b2.equals(b3) 将为所有逻辑上相等的b2、b3值打印true。
请注意 Integer 缓存不是出于性能原因,而是为了符合jls第5.1.7节的要求;必须为值-128到127(含)指定对象标识。
integer#valueof(int)也记录了这种行为:
通过缓存频繁请求的值,此方法可能会产生显著更好的空间和时间性能。此方法将始终缓存-128到127(含)范围内的值,并且可能缓存此范围之外的其他值。

arknldoa

arknldoa6#

它是与java相关的内存优化。
为了节省内存,java“重用”值在以下范围内的所有 Package 器对象:
所有布尔值(true和false)
所有字节值
从\u0000到\u007f的所有字符值(即十进制中的0到127)
从-128到127的所有短值和整数值。

lnlaulya

lnlaulya7#

看看integer.java,如果值介于-128和127之间,它将使用缓存池,所以 (Integer) 1 == (Integer) 1(Integer) 222 != (Integer) 222 ```
/**

  • Returns an {@code Integer} instance representing the specified
  • {@code int} value. If a new {@code Integer} instance is not
  • required, this method should generally be used in preference to
  • the constructor {@link #Integer(int)}, as this method is likely
  • to yield significantly better space and time performance by
  • caching frequently requested values.
  • This method will always cache values in the range -128 to 127,
  • inclusive, and may cache other values outside of this range.
  • @param i an {@code int} value.
  • @return an {@code Integer} instance representing {@code i}.
  • @since 1.5
    */
    public static Integer valueOf(int i) {
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high)
    return IntegerCache.cache[i + (-IntegerCache.low)];
    return new Integer(i);
    }

相关问题