Java 提供了俩个类型系统:基本类型、引用类型 使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们使用基本类型对象一样操作,就可以使用基本类型对应的包装类
4类8种如下图(可以百度)
基本数据类型与包装类:主要记住 Integer 和 Character
基本类型与对应的包装类对象之间,来回转换的过程称为“装箱”与“拆箱”
装箱:从基本数据类型转换为对应的包装类对象
拆箱:从包装类对象转换为对应的基本类型
这里以 Integer 包装类为例说明拆箱与装箱装箱:基本数值 --> 包装对象
// 构造方法 Integer integer1 = new Integer(1); // 静态方法 Integer integer3 = Integer.valueOf(1);拆箱:包装对象 --> 基本数值
// 成员方法 Integer对象.intValue() int i = integer1.intValue();自动装箱与自动拆箱:基本类型的数据和包装类之间可以自动的相互转换(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是指向不同的引用的。