拆装箱练习

博客主要围绕基本数据类型的自动拆装箱展开练习。介绍了通过Integer.MAX_VALUE获取int最值,对byte、short、float、double进行自动拆箱和装箱操作,还提及Double拆箱的写法,指出byte和Integer间不能自动拆箱和装箱,以及通过Byte获取byte最值。

int的最值可以通过其对应的封装类Integer.MAX_VALUE获取

package com.ly.chaizhuangxiang;

public class Int {
    public static void main(String[] args) {
        System.out.println("INT的最大值"+Integer.MAX_VALUE);
        System.out.println("INT的最小值"+Integer.MIN_VALUE);

    }
}

在这里插入图片描述

练习拆装箱

  1. 对byte,short,float,double进行自动拆箱和自动装箱
    byte
    装箱
 public static void main(String[] args) {
        byte i=1;

        //基本数据类型转换为封装类
        Byte by=new Byte(i);
        System.out.println(by);
    }

拆箱

        byte i=3;
        Byte by=new Byte(i);
        //封装类转换为基本类型,该种方式不会触发自动装箱
        byte i1=by.byteValue();
        //自动转换就叫拆箱
        int i2=by;
        System.out.println(i2);

short
装箱

public static void main(String[] args) {
        short i=2;

        //基本数据类型转换为封装类
        Short sh=new Short(i);
        System.out.println(sh);
    }

装箱

short i=5;
        Short sh=new Short(i);
        //封装类转换为基本类型
        short i1=sh.shortValue();
        //自动转换就叫拆箱
        short i2=sh;
        System.out.println(i2);

Double

public static void main(String[] args) {
        double d1=20.0;
        Double d2=null;
        d2=d1;//自动装箱
        d1=d2;//自动拆箱

        System.out.println("自动装箱"+d2);
        System.out.println("自动拆箱"+d1);
    }

拆箱有两种写法,第一种就会调用XXXValue()方法,但不会自动拆箱。

剩下的以此类比。

  1. byte和Integer之间能否进行自动拆箱和自动装箱
    不能.

  2. 通过Byte获取byte的最值

public static void main(String[] args) {
        System.out.println(Byte.MAX_VALUE);
        System.out.println(Byte.MIN_VALUE);
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值