zoukankan      html  css  js  c++  java
  • Java 包装类

    包装类的概念

    Java提供了两个类型系统:基本类型与引用类型
    使用基本类型在于效率,然而很多情况,会创建对象使用,因为对象可以做更多的功能,如果想要我们的基本类型像对象一样操作,就可以使用基本类型对应的包装类
    
    如下:
    
    基本类型                 对应的包装类(位于java.lang包中)
    byte                    Byte
    short                   Short
    int                     Integer
    long                    Long
    float                   Float
    double                  Double
    char                    Character
    boolean                 Boolean

    基本数据类型使用起来非常方便但是没有对应的方法来操作这些基本类型的数据
    可以使用一个类把基本类型的数据装起来,在类中定义一些方法,这个类叫做包装类
    我们可以使用类中的方法来操作这些基本类型的数据

    装箱与拆箱

    说明

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

    装箱

    /**
     * 以Integer为例
     * 装箱:把基本类型的数据,包装到包装类中(基本类型的数据->包装类)
     *      构造方法:
     *              Integer(int value)构造一个新分配的 Integer对象,它表示指定的int值。
     *              Integer(String s)构造一个新分配的 Integer对象,它表示 String参数所指示的int值。
     *              传递的字符串,必须是基本类型的字符串,否则会抛出昇常 如:"2018"正确,"a"抛昇常
     *      静态方法:
     *              static Integer valueOf(int i)返回一个表示指定的int值的 Integer实例。
     *              static Integer valueOf( String s)返回保存指定的 String的值的 Integer对象。
     *
     */
    public class DemoPack {
        public static void main(String[] args) {
            // 构造方法:
            // int
            Integer integer1 = new Integer(1);
            System.out.println("Integer(int value):" + integer1);
            // string
            Integer integer2 = new Integer("1");
            System.out.println("Integer(String s):" + integer2);
    
            // 静态方法
            // int
            Integer integer3 = Integer.valueOf(1);
            System.out.println("valueOf(int i):" + integer3);
            // string
            Integer integer4 = Integer.valueOf("1");
            System.out.println("valueOf(String s):" + integer3);
        }
    }
    暑促结果:
    Integer(int value):1
    Integer(String s):1
    valueOf(int i):1
    valueOf(String s):1

    拆箱

    /**
     * 拆箱:在包装类中取出基本类型的数据(包装类->基本类型的数据)
     * 成员方法:
     *         int involve()以int类型返回该Integer的值。
     */
    public class DemoUnpack {
        public static void main(String[] args) {
            // 装箱
            Integer integer = new Integer(1);
            System.out.println("Integer(int value):" + integer);
    
            // 拆箱
            int i = integer.intValue();
            System.out.println("int involve():" + i);
        }
    }
    输出结果:
    Integer(int value):1
    int involve():1

    自动装箱与自动拆箱

    /**
     * 自动装箱与自动拆箱:基本类型的效据和包装类之间可以自动的相互转换
     *                 JDK1.5之后出现的新特性
     */
    public class DemoPackUnpackAutomatic {
        public static void main(String[] args) {
            // 自动装箱:直接把int类型的整数赋值包装类Integer
            // i = 1;就相当于 Integer i = new Integer(1)
            Integer i = 1;
    
            // 自动拆箱:i是包装类,无法直接参与运算,可以自动转换为基本数据类型,在进行计算
            // i + 2 就相当于 i.intValue() + 2 = 3
            // i = i.intValue() + 2 = 3 又是一个自动装箱
            i = i + 2;
        }
    }

    基本类型与字符串类型之间的相互转换

    基本类型转字符串类型

    /**
     * 基本类型与字符串类型之间的相互转换
     * 基本类型->字符串( string)
     *               1.基本类型的值 + ""
     *               2.包装类的静态方法 toString(参数),不是Object类的toString()
     *                 如 static string toString(int)返回一个表示指定整数的string对象。
     *               3.string类的静态方法 valueOf(参数)
     *                 如 static String valueOf(int i)返回int参数的字符串表示形式。
     */
    public class DemoType {
        public static void main(String[] args) {
            // 基本类型->字符串( string)
            int i1 = 666;
            String s1 = i1 + "";
            s1 = s1 + 999;
            System.out.println("string1 = " + s1);
    
            String s2 = Integer.toString(666);
            s2 = s2 + 999;
            System.out.println("string2 = " + s2);
    
            String s3 = String.valueOf(666);
            s3 = s3 + 999;
            System.out.println("string3 = " + s3);
        }
    }
    输出结果:
    string1 = 666999
    string2 = 666999
    string3 = 666999

    字符串类型转基本类型

    /**
     * 字符串(string)->基本类型
     *      使用包装类的静态方法 parseXXX("字符串")
     *              如: Integer类: static int parseInt(String s)
     *                  Double类: static double parseDouble(String s)
     */
    public class DemoType2 {
        public static void main(String[] args) {
            // 基本类型->字符串( string)
            int i1 = 666;
            String s1 = i1 + "";
    
            // 字符串(string)->基本类型
            int i = Integer.parseInt(s1);
            i = i + 999;
            System.out.println("Integer.parseInt(Strong s) = " + i);
        }
    }
    输出结果:
    Integer.parseInt(String s) = 1665
  • 相关阅读:
    LeetCode "Top K Frequent Elements"
    LeetCode "Integer Break"
    HackerRank "Angry Children 2"
    HackerRank "Kitty and Katty"
    HackerRank "Minimum Penalty Path"
    HackerRank "Larry's Array"
    HackerRank "TBS Problem" ~ NPC
    HackerRank "Morgan and a String"
    HackerRank "Favorite sequence"
    Windows不分区VHD装Linux多系统(三):VM虚拟机安装ubuntu18.04
  • 原文地址:https://www.cnblogs.com/liyihua/p/12181685.html
Copyright © 2011-2022 走看看