Java Integer and Integer are equal

Integer is a packaging class (reference data type), int is a basic data type,

Integer a=12;
Integer b=12;
//a==b为true;
Integer c=1200;
Integer d=1200;
//c==d为false;
Integer e=new Integer(1);
Integer f=new Integer(1);
//e==f为false

Comparison reference data type needed equals () method compare equal
because cached Integer,
the Integer value does not exceed the comparison between -128 ~ == 127 is true,
if it exceeds a new Integer object will result == false;
in When comparing, you can use (a.intValue==b) to compare,
useIntegerwithintComparedWeaknessWill be automatically unboxed intointType, so the result isintType comparisonintType
summary: Comparison between objects cannot be used ==, including digital packaging classes, Integer, Long, Short, Character, Byte, there is a cache mechanism, a number greater than the corresponding buffer pool will be a new object, you can not use = =, if it is smaller than the buffer pool, the wrapper class will not create a new object

Guess you like

Origin blog.csdn.net/qq_41844287/article/details/94616265