自动装箱和自动拆箱(包装类)

一、包装类

为什么要使用包装类?

Java语言是一个面向对象的语言,但是Java中的基本数据类型不是面向对象的,这在实际使用时有许多的不便,为了解决这个不足,在设计类时,为每个基本数据类型设计了一个对应的类进行代表,因为基本类型有八个,所以它们所对应的包装类就有八个。

包装类:这些类封装了一个相应的基本数据类型数值,并为其提供了一系列操作方法。

基本数据类型封装类:

基本数据类型 包装类
byte Byte
short Short
char Character
int Integer
long Long
float Float
double Double
boolean Boolean

二、自动装箱和自动拆箱

自动装箱:将基本数据类型转化为包装类类型,装箱时自动调用Integer的valueof(int)方法

自动拆箱:将包装类类型转化为基本数据类型,拆箱时自动调用Integer的intValue方法

/*自动装箱和自动拆箱*/
public class TestInteger2 {
    public static void main(String[] args) {
           int a= 10;
           //自动装箱
           //把基本类型转为包装类型,产生了一个Integer类的对象
           Integer ba = new Integer(a);//通过构造方法创建一个Integer类的对象
           Integer ba1 = Integer.valueOf(a);//通过调用Integer.valueOf()
           //自动拆箱
           //把包装类型,转为基本类型,取出包装类对象中的基本类型值
           int b = ba.intValue();
    }
}

 

/*自动装箱和自动拆箱如何用?语法规范*/
public class TestInteger3 {
    public static void main(String[] args) {
         /*
         把基本类型直接赋给引用类型,产生了一个Integer类的对象
         称为自动装箱
         * */
           int a= 10;//可以打断点强制进去看具体调用哪个方法
           //通过调用Integer.valueOf() 隐式的
           Integer ba = a;
         /*
         * 把包装类型转为基本类型,称为自动拆箱
         * */
           Integer c = new Integer(10);
         //拆箱时,默认会调用c.intValue();取出其中的int值 是隐式的.
           int dc = c;
    }
}

 反编译为:

/*自动装箱和自动拆箱如何用?语法规范*/
public class Test {
    public static void main(String[] args) {
           int a= 10;
           //通过调用Integer.valueOf() 隐式的
           Integer ba = Integer.valueOf(a);
        
           Integer c = new Integer(10);
          //拆箱时,默认会调用c.intValue();取出其中的int值 是隐式的.
           int dc = c.intValue;
    }
}

三、 自动装箱产生的问题

package com.ffyc.javaapi.basepackage;

public class TestInteger4 {
     /*
     但是自动装箱时会有一个问题,在比较值大小时128和127是不同的,和底层源码有关系
     * */
    public static void main(String args[]){

        /*
          Integer a = 127;  默认会调用 Integer Integer.valueOf(int i){ }
                在Integer类中为了节省空间,对-128--+127之间的256个对象进行缓存(数组)
                在-128 +127之间的时候,直接从数组获取,如果值相同的时候,取到的对象是相同的.
                如果不在-128 +127之间, 每次都会创建一个新的对象
        * public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
      }*/
        Integer a = 127;
        Integer b = 127;
        System.out.println(a==b);//true
        Integer c = 128;
        Integer d = 128;
        System.out.println(c==d);//false
        System.out.println(c.equals(d));//true

        Integer e = new Integer(128);
        Integer f = new Integer(128);
        System.out.println(e==f);//false 因为e和f都是新new出来的,对象地址不相等
        System.out.println(e.equals(f));//true
    }
}

底层源码:

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

 在通过valueOf方法创建Integer对象的时候,如果数值在[-128,127]之间,便返回指向IntegerCache.cache中已经存在的对象的引用;否则创建一个新的Integer对象

所以一般我们用equals()来比较值是否一致,因为Integer中已经重写了equals()。

你可能感兴趣的:(java,装箱,拆箱)