zoukankan      html  css  js  c++  java
  • 数字与字符串系列教材 (一)- Java中基本类型的装箱和拆箱

    步骤1:封装类
    步骤2:Number类
    步骤3:基本类型转封装类
    步骤4:封装类转基本类型
    步骤5:自动装箱
    步骤6:自动拆箱
    步骤7:int的最大值,最小值
    步骤8:练习-装箱拆箱
    步骤9:答案-装箱拆箱

    步骤 1 : 封装类

    所有的基本类型,都有对应的类类型 
    比如int对应的类是Integer 
    这种类就叫做封装类

    package digit;

    public class TestNumber {

        public static void main(String[] args) {

            int i = 5;

             

            //把一个基本类型的变量,转换为Integer对象 

            Integer it = new Integer(i);

            //把一个Integer对象,转换为一个基本类型的int

            int i2 = it.intValue();

             

        }

    }

    步骤 2 : Number类

    数字封装类有 
    Byte,Short,Integer,Long,Float,Double 
    这些类都是抽象类Number的子类

    Number类

    package digit;

    public class TestNumber {

        public static void main(String[] args) {

            int i = 5;

             

            Integer it = new Integer(i);

            //Integer是Number的子类,所以打印true

            System.out.println(it instanceof Number);

        }

    }

    步骤 3 : 基本类型转封装类

    package digit;

    public class TestNumber {

        public static void main(String[] args) {

            int i = 5;

            //基本类型转换成封装类型

            Integer it = new Integer(i);

             

        }

    }

    步骤 4 : 封装类转基本类型

    package digit;

    public class TestNumber {

        public static void main(String[] args) {

            int i = 5;

            //基本类型转换成封装类型

            Integer it = new Integer(i);

             

            //封装类型转换成基本类型

            int i2 = it.intValue();

             

        }

    }

    步骤 5 : 自动装箱

    不需要调用构造方法,通过=符号自动把 基本类型 转换为 类类型 就叫装箱

    package digit;

    public class TestNumber {

        public static void main(String[] args) {

            int i = 5;

            //基本类型转换成封装类型

            Integer it = new Integer(i);

             

            //自动转换就叫装箱

            Integer it2 = i;

             

        }

    }

    步骤 6 : 自动拆箱

    不需要调用Integer的intValue方法,通过=就自动转换成int类型,就叫拆箱

    package digit;

      

    public class TestNumber {

      

        public static void main(String[] args) {

            int i = 5;

      

            Integer it = new Integer(i);

              

            //封装类型转换成基本类型

            int i2 = it.intValue();

             

            //自动转换就叫拆箱

            int i3 = it;

              

        }

    }

    步骤 7 : int的最大值,最小值

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

    package digit;

      

    public class TestNumber {

      

        public static void main(String[] args) {

            //int的最大值 

            System.out.println(Integer.MAX_VALUE);

            //int的最小值       

            System.out.println(Integer.MIN_VALUE);

              

        }

    }


    更多内容,点击了解: https://how2j.cn/k/number-string/number-string-wrap/22.html

  • 相关阅读:
    问卷调查--来自20145320周岐浩
    第一次尝试编写java
    写在开始编写Java之前(2)——Java的环境
    写在开始编写Java之前(1)——Java的跨平台性
    一步一步实现JS拖拽插件
    序列图像三维重建 学习流水账
    linux配置虚拟机网络环境(老师要求的host-only)
    递归函数时间复杂度分析(转)
    什么是static?什么是final?
    复习java数据库操作的总结
  • 原文地址:https://www.cnblogs.com/Lanht/p/12615532.html
Copyright © 2011-2022 走看看