解释一下自动装箱和自动拆箱?

作者:程序员库森
链接:https://www.nowcoder.com/discuss/601272
来源:牛客网
 

自动装箱:将基本数据类型重新转化为对象

复制代码

1

2

3

4

5

6

public class Test { 

    public static void main(String[] args) { 

        // 声明一个Integer对象,用到了自动的装箱:解析为:Integer num = Integer.valueOf(9);

        Integer num = 9;

    

}

9是属于基本数据类型的,原则上它是不能直接赋值给一个对象Integer的。但jdk1.5 开始引入了自动装箱/拆箱机制,就可以进行这样的声明,自动将基本数据类型转化为对应的封装类型,成为一个对象以后就可以调用对象所声明的所有的方法。

自动拆箱:将对象重新转化为基本数据类型

复制代码

1

2

3

4

5

6

7

8

9

public class Test { 

       public static void main(String[] args) { 

           / /声明一个Integer对象

           Integer num = 9;

 

           // 进行计算时隐含的有自动拆箱

           System.out.print(num--);

       

   }

因为对象时不能直接进行运算的,而是要转化为基本数据类型后才能进行加减乘除

9. int 和 Integer 有什么区别?

  • Integer是int的包装类;int是基本数据类型;
  • Integer变量必须实例化后才能使用;int变量不需要;
  • Integer实际是对象的引用,指向此new的Integer对象;int是直接存储数据值 ;
  • Integer的默认值是null;int的默认值是0。

10. 两个new生成的Integer变量的对比

由于Integer变量实际上是对一个Integer对象的引用,所以两个通过new生成的Integer变量永远是不相等的(因为new生成的是两个对象,其内存地址不同)。

复制代码

1

2

3

Integer i = new Integer(10000);

Integer j = new Integer(10000);

System.out.print(i == j); //false

11. Integer变量和int变量的对比

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

复制代码

1

2

3

4

5

int a = 10000;

Integer b = new Integer(10000);

Integer c=10000;

System.out.println(a == b); // true

System.out.println(a == c); // true

12. 非new生成的Integer变量和new Integer()生成变量的对比

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

复制代码

1

2

3

Integer b = new Integer(10000);

Integer c=10000;

System.out.println(b == c); // false

13. 两个非new生成的Integer对象的对比

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

复制代码

1

2

3

4

5

6

7

Integer i = 100;

Integer j = 100;

System.out.print(i == j); //true

 

Integer i = 128;

Integer j = 128;

System.out.print(i == j); //false

当值在 -128 ~ 127之间时,java会进行自动装箱,然后会对值进行缓存,如果下次再有相同的值,会直接在缓存中取出使用。缓存是通过Integer的内部类IntegerCache来完成的。当值超出此范围,会在堆中new出一个对象来存储。

给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf,源码如下:

复制代码

1

2

3

public static Integer valueOf(String s, int radix) throws NumberFormatException {

        return Integer.valueOf(parseInt(s,radix));

    }

复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

 * (1)在-128~127之内:静态常量池中cache数组是static final类型,cache数组对象会被存储于静态常量池中。

 * cache数组里面的元素却不是static final类型,而是cache[k] = new Integer(j++),

 * 那么这些元素是存储于堆中,只是cache数组对象存储的是指向了堆中的Integer对象(引用地址)

 *

 * (2)在-128~127 之外:新建一个 Integer对象,并返回。

 */

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);

    }

IntegerCache是Integer的内部类,源码如下:

复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

/**

  * 缓存支持自动装箱的对象标识语义 -128和127(含)。

  * 缓存在第一次使用时初始化。 缓存的大小可以由-XX:AutoBoxCacheMax = <size>选项控制。

  * 在VM初始化期间,java.lang.Integer.IntegerCache.high属性可以设置并保存在私有系统属性中

 */

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) {

            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);

        }

        high = h;

 

        cache = new Integer[(high - low) + 1];

        int j = low;

        for(int k = 0; k < cache.length; k++) {

            cache[k] = new Integer(j++); // 创建一个对象

        }

    }

 

    private IntegerCache() {}

}

 


 

 

Integer x = new Integer(123);
Integer y = new Integer(123);
System.out.println(x == y);    // false
Integer z = Integer.valueOf(123);
Integer k = Integer.valueOf(123);
System.out.println(z == k);   // true
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

 

 

在使用这些基本类型对应的包装类型时,如果该数值范围在缓冲池范围内,就可以直接使用缓冲池中的对象。

在 jdk 1.8 所有的数值类缓冲池中,Integer 的缓冲池 IntegerCache 很特殊,这个缓冲池的下界是 - 128,上界默认是 127,但是这个上界是可调的,在启动 jvm 的时候,通过 -XX:AutoBoxCacheMax=<size> 来指定这个缓冲池的大小,该选项在 JVM 初始化的时候会设定一个名为 java.lang.IntegerCache.high 系统属性,然后 IntegerCache 初始化的时候就会读取该系统属性来决定上界

  • 装箱:将基本类型用包装器类型包装起来

  • 拆箱:将包装器类型转换为基本类型

    这个地方有很多易混淆的地方,但在面试中问到的频率一般,笔试的选择题中经常出现,还有一个String创建对象和这个比较像,很容易混淆,在下文可以看到

  • wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
     

    编译器会在自动装箱过程调用 valueOf() 方法,因此多个值相同且值在缓存池范围内的 Integer 实例使用自动装箱来创建,那么就会引用相同的对象。

    Integer m = 123;
    Integer n = 123;
    System.out.println(m == n); // true
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    new Integer(123) 与 Integer.valueOf(123) 的区别在于:

  • new Integer(123) 每次都会新建一个对象;
  • Integer.valueOf(123) 会使用缓存池中的对象,多次调用会取得同一个对象的引用。
  • 基本类型对应的缓冲池如下:

  • boolean values true and false
  • all byte values
  • short values between -128 and 127
  • int values between -128 and 127
  • char in the range \u0000 to \u007F

  • 下面这段代码的输出结果是什么?

    public class Main {
        public static void main(String[] args) {
            
                Integer a = 100;
                Integer b = 100;
                Integer c = 128;
                Integer d = 128;
    
                System.out.println(a==b);
                System.out.println(c==d);
        }
    }
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
    true
    false
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    很多人看到这个结果会很疑惑,为什么会是一个true一个flase.其实从源码中可以很容易找到原因.首先找到Integer方法中的valueOf方法

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

    可以看到当不满足if语句中的条件,就会重新创建一个对象返回,那结果必然不相等。继续打开IntegerCache可以看到

        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() {}
          }
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    代码挺长,大概说的就是在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象。所以上面代码中ab相等,cd不相等。

  • 再看下面的代码会输出什么

    public class Main {
        public static void main(String[] args) {
    
                Double a = 1.0;
                Double b = 1.0;
                Double c = 2.0;
                Double d = 2.0;
    
                System.out.println(a==b);
                System.out.println(c==d);
    
        }
    }
    
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
    flase
    flase
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    采用同样的方法,可以看到DoublevalueOf方法,每次返回都是重新new一个新的对象,所以上面代码中的结果都不想等。

    public static Double valueOf(double d) {
              return new Double(d);
    }
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
  • 最后再看这段代码的输出结果

    public class Main {
        public static void main(String[] args) {
    
            Boolean a = false;
            Boolean b = false;
            Boolean c = true;
            Boolean d = true;
    
            System.out.println(a==b);
            System.out.println(c==d);
        }
    }
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
    true
    true
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    老方法继续看valueOf方法

    public static Boolean valueOf(boolean b) {
            return (b ? TRUE : FALSE);
        }
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    再看看TRUEFALSE是个什么东西,是两个静态成员属性。

    public static final Boolean TRUE = new Boolean(true);
    public static final Boolean FALSE = new Boolean(false);
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

**说下结论 **:IntegerShortByteCharacterLong这几个类的valueOf方法的实现是类似的。DoubleFloatvalueOf方法的实现是类似的。然后是BooleanvalueOf方法是单独一组的。

  • Integer i = new Integer(xxx)Integer i =xxx的区别

    这两者的区别主要是第一种会触发自动装箱,第二者不会

    最后看看下面这段程序的输出结果

    public class Main {
        public static void main(String[] args) {
            Integer a = 1;
            Integer b = 2;
            Integer c = 3;
            Long g = 3L;
            int int1 = 12;
            int int2 = 12;
            Integer integer1 = new Integer(12);
            Integer integer2 = new Integer(12);
            Integer integer3 = new Integer(1);
    
            System.out.println("c==(a+b) ->"+ (c==(a+b)));
            System.out.println("g==(a+b) ->" + (g==(a+b)));
            System.out.println( "c.equals(a+b) ->" + (c.equals(a+b)));
            System.out.println( "g.equals(a+b) ->" + (g.equals(a+b)));
            System.out.println("int1 == int2 -> " + (int1 == int2));
            System.out.println("int1 == integer1 -> " + (int1 == integer1));
            System.out.println("integer1 == integer2 -> " + (integer1 == integer2));
            System.out.println("integer3 == a1 -> " + (integer3 == a));
        }
    }
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
    c==(a+b) ->true
    g==(a+b) ->true
    c.equals(a+b) ->true
    g.equals(a+b) ->false
    int1 == int2 -> true
    int1 == integer1 -> true
    integer1 == integer2 -> false
    integer3 == a1 -> false
    
    wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

    下面简单解释这些结果。

    1.当 "=="运算符的两个操作数都是包装器类型的引用,则是比较指向的是否是同一个对象,而如果其中有一个操作数是表达式(即包含算术运算)则比较的是数值(即会触发自动拆箱的过程)。所以c==a+bg==a+btrue

    2.而对于equals方法会先触发自动拆箱过程,再触发自动装箱过程。也就是说a+b,会先各自调用intValue方法,得到了加法运算后的数值之后,便调用Integer.valueOf方法,再进行equals比较。所以c.equals(a+b)true。而对于g.equals(a+b)a+b会先拆箱进行相加运算,在装箱进行equals比较,但是装箱后为IntegergLong,所以g.equals(a+b)false

    3.int1 == int2true无需解释,int1 == integer1,在进行比较时,integer1会先进行一个拆箱操作变成int型在进行比较,所以int1 == integer1true

    4.integer1 == integer2->falseinteger1integer2都是通过new关键字创建的,可以看成两个对象,所以integer1 == integer2 为falseinteger3 == a1-> false , integer3是一个对象类型,而a1是一个常量它们存放内存的位置不一样,所以integer3 == a1false,具体原因可学习下java的内存模型。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值