Java 基础 包装类 (四类八种的包装类)拆箱、装箱 JVM缓存整型

本文详细介绍了Java中基本类型与包装类的概念,包括装箱与拆箱的过程及自动装箱拆箱的使用场景,同时探讨了不同情况下对象比较的行为及其背后的原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

包装类

概述

Java 提供了俩个类型系统:基本类型、引用类型
使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们使用基本类型对象一样操作,就可以使用基本类型对应的包装类

4类8种如下图(可以百度)
在这里插入图片描述

基本数据类型与包装类:主要记住 Integer 和 Character
在这里插入图片描述

装箱与拆箱

基本类型与对应的包装类对象之间,来回转换的过程称为“装箱”与“拆箱”

  • 装箱:从基本数据类型转换为对应的包装类对象
  • 拆箱:从包装类对象转换为对应的基本类型

这里以 Integer 包装类为例说明拆箱与装箱
装箱:基本数值 --> 包装对象

// 构造方法
Integer integer1 = new Integer(1);
// 静态方法
Integer integer3 = Integer.valueOf(1);

拆箱:包装对象 --> 基本数值

// 成员方法 Integer对象.intValue()
int i = integer1.intValue();

自动装箱与拆箱 (JDK1.5 之后新特性)

自动装箱与自动拆箱:基本类型的数据和包装类之间可以自动的相互转换(JDK1.5之后新特性)

/*
  * 自动装箱:把int->包装类
  *  Integer in = 1; 等价于 Integer in = new Integer(1);
  * */
 Integer in = 1;
 /*
  * 自动拆箱:in 是包装类,先转为int 在计算
  *  in +3;  自动拆箱 in.intValue() +3 = 4
  *   in =   in.intValue() +3 ; 自动装箱
  * */
 in = in + 3;

使用场景: ArrayList

ArrayList<Integer> list = new ArrayList<>();

list.add(1); // 自动装箱,等价于下面代码
list.add(new Integer(1));

int i = list.get(1);//自动拆箱,等价于下面代码
int i1 = list.get(1).intValue();

在Java中使用自动装箱与拆箱应当注意的问题 (含面试题)

对象相等比较:

public class AutoboxingTest {
    public static void main(String args[]) {
        // Example 1: == comparison pure primitive – no autoboxing
        int i1 = 1;
        int i2 = 1;
        System.out.println("i1==i2 : " + (i1 == i2)); // true

        // Example 2: equality operator mixing object and primitive
        Integer num1 = 1; // autoboxing
        int num2 = 1;
        System.out.println("num1 == num2 : " + (num1 == num2)); // true

        // Example 3: special case - arises due to autoboxing in Java
        Integer obj1 = 1; // autoboxing will call Integer.valueOf()
        Integer obj2 = 1; // same call to Integer.valueOf() will return same cached Object
        System.out.println("obj1 == obj2 : " + (obj1 == obj2)); // true

        // Example 4: equality operator - pure object comparison
        Integer one = new Integer(1); // no autoboxing
        Integer anotherOne = new Integer(1);
        System.out.println("one == anotherOne : " + (one == anotherOne)); // false
    }
}

/** Output:
i1==i2 : true
num1 == num2 : true
obj1 == obj2 : true
one == anotherOne : false

在Example 2中,比较是一个对象和一个原始值,出现这种情况比较的应该是对象的值。
让人感到困惑的Example 3,在一开始我们说过,用于对象间的比较时,比较的是它们的引用,那么为什么obj1 == obj2返回的结果却是true?这是一种极端情况,处于节省内存的考虑,JVM会缓存-128到127的Integer对象。也就是说,在创建obj1对象时,会进行自动装箱操作,并且将其对象保存至缓存中,在创建obj2对象时,同样会进行自动装箱操作,然后在缓存中查找是否有相同值的对象,如果有,那么obj2对象就会指向obj1对象。obj1和obj2实际上是同一个对象。所以使用""比较返回true
而Example 4,是通过使用构造器来创建对象的,而没有发生自动装箱操作,不会执行缓存策略,故one和anotherOne是指向不同的引用的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值