指向同一 Integer 对象的变量之间的比较

发布于 2024-09-07 02:35:12 字数 320 浏览 4 评论 0原文

当前程序的输出是“Strange”。但这两个变量共享相同的引用。为什么第二个和第三个比较不正确?

Integer a;
Integer b;
a = new Integer(2);
b = a;
if(b == a) {
    System.out.println("Strange");
}
a++;
if(b == a) {
    System.out.println("Stranger");
}
a--;
if(b == a) {
    System.out.println("Strangest");
}

输出:奇怪

The output of current program is "Strange". But both the variables share the same reference. Why are the second and third comparisons not true?

Integer a;
Integer b;
a = new Integer(2);
b = a;
if(b == a) {
    System.out.println("Strange");
}
a++;
if(b == a) {
    System.out.println("Stranger");
}
a--;
if(b == a) {
    System.out.println("Strangest");
}

Output: Strange

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

止于盛夏 2024-09-14 02:35:12

这是自动装箱的产物,也是 Java 中 Integer 不可变的事实。

a++a-- 大致翻译为这样。

int intA = a.getInt( );
intA++;
a = Integer.valueOf( intA ); // this is a reference different from b

That's the artifact of autoboxing and a fact that Integer is immutable in Java.

The a++ and a-- are translated to roughly this.

int intA = a.getInt( );
intA++;
a = Integer.valueOf( intA ); // this is a reference different from b
时光磨忆 2024-09-14 02:35:12
  • Strage - 很明显,两个变量指向同一个对象

  • 由于自动装箱,所以不是 StrangerInteger 是不可变的,因此对其进行的每个操作都会创建一个新实例。

  • 不是奇怪,因为前面的一点,并且因为您使用了new Integer(..),它忽略了用于字节范围的缓存。如果您最初使用 Integer.valueOf(2),那么将使用缓存的 Integer 并且也会打印 Strangest

  • Strage - it's obvious, the two variables point to the same object

  • not Stranger because of autoboxing. Integer is immutable, so each operation on it creates a new instance.

  • not Strangest, because of the previous point, and because you have used new Integer(..) which ignores the cache that is used for the byte range. If you use Integer.valueOf(2) initially, then the cached Integers will be used and Strangest will also be printed.

饭团 2024-09-14 02:35:12

Integer 对象是不可变的,现有对象的任何更改都会创建一个新对象。因此,在 a++ 之后,将创建一个新对象,并且 a 将开始指向该新对象,而 b 仍指向旧对象。因此,在 a++ 之后,ab 指向不同的对象,并且 a == b 将始终返回 false 。

关于提到的例子:

Integer a; //created Integer reference   
Integer b;  //created Integer reference  
a = new Integer(2);//created new Integer Object and a reference is assigned to that new object   
b = a;//b also start pointing to same Integer object   
if(b == a) { // b==a will be true as both are pointing to same object   
System.out.println("Strange");   
}
a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false   
if(b == a) { 
System.out.println("Stranger"); 
}   
a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false   
if(b == a) { 
System.out.println("Strangest");
}

An Integer object is immutable, any change in an existing object will create a new object. So after a++, a new object will be created and a will start pointing to that new object while b is still pointing to the old object. Hence, after a++, a and b are pointing to different objects and a == b will always return false.

with respect to the mentioned example :

Integer a; //created Integer reference   
Integer b;  //created Integer reference  
a = new Integer(2);//created new Integer Object and a reference is assigned to that new object   
b = a;//b also start pointing to same Integer object   
if(b == a) { // b==a will be true as both are pointing to same object   
System.out.println("Strange");   
}
a++; //after a++ , a new Integer object will be created (due to Integer immutablity and a will point to that new object while b is still pointing to old), so b==a will be false   
if(b == a) { 
System.out.println("Stranger"); 
}   
a--; //again a new Integer Object will be created and now a will start pointing to that new Object , so b==a will be false   
if(b == a) { 
System.out.println("Strangest");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文