java拆箱 装箱 一篇文章就够了

java拆箱装箱

1.java的基本数据类型

Java 基本类型共有八种,基本类型可以分为三类:

函数的局部变量在堆栈上,类成员变量在堆上,还有常数在常量池

  1. 字符类型 char
  2. 布尔类型 boolean
  3. 数值类型 byte、short、int、long、float、double。

数值类型又可以分为整数类型 byte、short、int、long 和浮点数类型 float、double。
Java 中的数值类型不存在无符号的,它们的取值范围是固定的,不会随着机器硬件环
境或者操作系统的改变而改变。
实际上 , java中还存在另外一 种基本类型void ,它也有对应的包装类java.lang.Void,不过我们无法直接对它们进行操作。

基本数据类型有什么好处

new的对象都存在于中,通过栈的中的reference (引用)来使用这些对象非常浪费资源。
对于经常用到的类型,如 int 等,如果我们每次使用这种变量的时候都需要 new 一个Java 对象的话,就会比较笨重。所以,和 C++一样,Java 提供了基本数据类型,这种数据的变量不需要使用 new 创建,他们不会在堆上创建,而是直接在栈内存中存储,因此会更加高效。

超出范围怎么办
int i = Integer.MAX_VALUE;
int j = Integer.MAX_VALUE;
int k = i + j;
System.out.println("i (" + i + ") + j (" + j + ") = k (" + k + ")");

输出结果:i (2147483647) + j (2147483647) = k (-2)
这就是发生了溢出,溢出的时候并不会抛异常,也没有任何提示。所以,在程序中,使
用同类型的数据进行运算的时候,一定要注意数据溢出的问题。

2. 什么是拆箱装箱

 //自动装箱
 Integer a = 99;

//自动拆箱
 int b = a;

3.什么时候拆箱装箱

  1. 将基本数据类型放入集合类
List<Integer> list = new ArrayList<>();
list.add(1);   //list.add(Intege.valueOf(1));
  1. 包装类型和基本类型大小比较
Ingeter a = 1 ;
//  System.out.println (a.intValue == 1 ? "真":"假")
System.out.println (a == 1 ? "真":"假") 
Boolean bo = false;
// System.out.println (bo.booleanValue ? "真":"假") 
System.out.println (bo == 1 ? "真":"假") 
  1. 包装类型运算
/**
Integer i = Integer.valueOf(1);
Integer j = Integer.valueOf(2);
System.out.println(i.intValue() + j.intValue());
**/
Integer i = 1;
Integer j = 2;
System.out.println( i + j );
  1. 三目运算符
boolean bool = true ;
Ingeter i = 0;
int j = 1;
int k = bool ? i : j ;
/**
int k = bool ? i.intValue : j ;
**/
  1. 函数返回值
//自动拆箱
public int getNum1(Integer num) {
	return num;
}
//自动装箱
public Integer getNum2(int num) {
	return num;
}
4. Ingeter 的缓存机制

内部类源码 和其他类型一样都是包装类型的缓存

private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        static {
        	// 高值可由属性配置 
        	// java -D java.lang.Integer.IntegerCache.high=1000 TestAutoBoxCache 
        	//-XX:AutoBoxCacheMax=NNN参数即可将Integer的自动缓存区间设置为[-128,NNN]
            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() {}
    }
// valueOf 方法 从缓存中取对象  在缓存范围内是同一个对象
public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
// valueOf 方法 从缓存中取对象  在缓存范围内是同一个对象
public int intValue() {
        return value;
    }
//初始化时自动装箱 相当于 Integet.valueOf(this.value) = Integet.valueOf(value)
 public Integer(int value) {
        this.value = value;
    }

Integer 维护了一个 private final int value; 他代表当前Integer所对应的基本类型 ,装箱或者初始化时进项value初始化,拆箱时返回value

5.自动拆箱装箱是如何实现的
Integer i = 10 ;
int j = i ;

反编译结果之后

public class com.wintec.Main {
  public com.wintec.Main();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: bipush        10
       2: invokestatic  #2                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       5: astore_1
       6: aload_1
       7: invokevirtual #3                  // Method java/lang/Integer.intValue:()I
      10: istore_2
      11: return
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1999

每人一点点,明天会更好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值