博主
258
258
258
258
专辑

第二节 自动装箱、拆箱

亮子 2021-09-10 09:46:53 3757 0 0 0
  • 方便包装类和基本数据类型之间的转换。
  • 装箱:把基本数据类型转换成包装类。
  • 拆箱:把包装类转换成基本数据类型。

1、代码演示

package com.shenmazong.core;

/**
 * @program: server-java-demo
 * @description: 装箱与拆箱演示
 * @author: 亮子说编程
 * @create: 2020-10-15 09:58
 **/
public class TypeInstall01 {

    public static void main(String[] args) {

        //-- 自动装箱
        Integer age = 1;

        //-- 手动装箱
        Integer score = new Integer(1);

        //-- 自动拆箱
        int myAge = age;

        //-- 手动拆箱
        int myScore = score.intValue();
    }
}

2、相关面试题

1)、请写出下面程序的输出结果

public class DataType02 {

    public static void main(String[] args) {

        Integer a = 100;
        Integer b = 100;
        System.out.println(a == b); // false
        System.out.println(a.equals(b)); // true

        Integer x = 128;
        Integer y = 128;
        System.out.println(x == y); // false
        System.out.println(x.equals(y)); // true
    }
}