Boxed Primitives and Equivalence

后端 未结 4 1009
伪装坚强ぢ
伪装坚强ぢ 2020-12-06 10:36

So I was asked this question today.

Integer a = 3;
Integer b = 2;
Integer c = 5;
Integer d = a + b;
System.out.println(c == d);

What will t

4条回答
  •  渐次进展
    2020-12-06 11:05

    It's because some of the (auto-boxed) Integers are cached, so you're actually comparing the same reference -- this post has more detailed examples and an explanation.

提交回复
热议问题