i
与Integer x1 = 5;
Integer x1 = new Integer(5);
此代码显示两次不显示Same对象,正如我预期的那样。为什么呢?
PS:规则是:为了节省内存,当它们的原始值相同时,以下包装器对象的两个实例总是==:Boolean;字节;字符从\ u到\ u007f;短和整数从-128到127
答案 0 :(得分:2)
// Prints because of autoboxing ie it converts to integer value.
if (x1 == x2) System.out.println("Same object");
/*
* Doesn't prints because of creating an object(x3) using Integer wrapper class
* which is different from other object x4. ie both are different object
* pointing to different memory location in memory.
*/
if (x3 == x4) System.out.println("Same object");