- public class Demo {
- public static void main(String[] args) {
- Integer intObj1 = 20;
- Integer intObj2 = 20;
- System.out.println("intObj1 == intObj2 : " + (intObj1 == intObj2));//true
-
- Integer intObj3 = 127;
- Integer intObj4 = 127;
-
- System.out.println("intObj3 == intObj4 : " + (intObj3 == intObj4));//true
-
- Integer intObj5 = 128;
- Integer intObj6 = 128;
- System.out.println("intObj5 == intObj6 : " + (intObj5 == intObj6));//false
-
- Integer intObj7 = new Integer(127);
- Integer intObj8 = new Integer(127);
- System.out.println("intObj7 == intObj8 : " + (intObj7 == intObj8));//false
-
-
- }
- }
复制代码 结论Integer会自动创建-128到127的对象 当数值在这个范围内的时候 直接使用对象的引用
如果数值超过这个范围 会分贝创建新的对象
|
|