zoukankan      html  css  js  c++  java
  • java中int和Integer的区别

    Java中int类型和Integer类型的区别:

    1.int是Java的一种基本数据类型,Integer是int的包装类(引用类型)。

    2.int变量不需要实例化即可使用,而Integer变量必须要实例化后才能使用。(Integer实际上是引用类型,因此必须实例化才能使用,比如说使用关键字new。会看到有Integer num = 300的写法实际上也是实例化,因为Java提供了自动装箱拆箱的机制。)

    3.int是直接存储数据值,而Integer实际上是对象的引用,存储的是指向实际值的指针。

    4.int的默认值是0,Integer的默认值是null。

    int变量和Integer变量的==比较

    1.由于Integer变量实际上是对一个Integer对象的引用,所有两个通过new生成的Integer变量用==比较永远是不相等的(new生成的是两个对象,内存地址不同,==比较的是内存地址)。

    Integer num1 = new Integer(100);
    Integer num2 = new Integer(100);
    System.out.println(num1 == num2); // false,==比较的是内存地址

    2.Integer变量和int变量比较时,只要两个变量的值是相等的,则结果为true(包装类Integer和基本数据类型int比较的时候,Java会自动拆箱为int,然后进行比较,实际上就变为两个int变量的比较)。

    Integer num1 = new Integer(100);
    int num2 = 100;
    System.out.println(num1 == num2); // true

    3.非new生成的Integer变量和new Integer()生成的变量比较时,结果为false(非new生成的Integer变量指向的是Java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同)。

    Integer num1 = new Integer(100);
    Integer num2 = 100;
    System.out.println(num1 == num2); // false

    4.对于两个非new生成的Integer对象,进行比较时,如果两个变量的值在区间-128到127之间,则比较结果为true;如果两个变量的值不在此区间,则比较结果为false。

    Integer num1 = 100;
    Integer num2 = 100;
    Integer num3 = 128;
    Integer num4 = 128;
    System.out.println(num1 == num2); // true
    System.out.println(num3 == num4); // false

    这是因为Java在编译非new生成的Integer对象的时候,实际上是调用了Integer类的静态方法Integer.valueOf()方法。

    public static Integer valueOf(int i){
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high){
            return IntegerCache.cache[i + (-IntegerCache.low)];
        }
        return new Integer(i);
    }

    通过查看Integer.valueOf()方法的源码可以看出,Java对于-128到127之间的数会进行缓存(常量池),因此在这个范围内的数值实际上是指向缓存(常量池)中的对象,当不在这个范围的时候,才会在堆中new一个新的对象。

    IntegerCache是Integer类的内部类,好好研究它的实现有助于对Integer字面量的理解。

    private static class IntegerCache {
            static final int low = -128;
            static final int high;
            static final Integer cache[];
    
            static {
                // high value may be configured by property
                int h = 127;
                String integerCacheHighPropValue =
                    sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
                if (integerCacheHighPropValue != null) {
                    try {
                        int i = parseInt(integerCacheHighPropValue);
                        i = Math.max(i, 127);
                        // Maximum array size is Integer.MAX_VALUE
                        h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                    } catch( NumberFormatException nfe) {
                        // If the property cannot be parsed into an int, ignore it.
                    }
                }
                high = h;
    
                cache = new Integer[(high - low) + 1];
                int j = low;
                for(int k = 0; k < cache.length; k++)
                    cache[k] = new Integer(j++);
    
                // range [-128, 127] must be interned (JLS7 5.1.7)
                assert IntegerCache.high >= 127;
            }
    
            private IntegerCache() {}
        }

    "不知道什么时候开始,听故事的人变成了故事里的人。"

  • 相关阅读:
    HDU 3944 DP? (Lucas定理)
    Gym 100548F Color (数论容斥原理+组合数)
    Gym 100548K Last Defence (数论)
    Gym 100548A Built with Qinghuai and Ari Factor (水题)
    npx命令
    开源许可证(转载)
    CMD命令
    学习ES6的全部特性
    深入浅出数据库索引(转)
    .net基础总复习(3)
  • 原文地址:https://www.cnblogs.com/yanggb/p/10727092.html
Copyright © 2011-2022 走看看