基本类型的包装类

时间:2019-10-31
本文章向大家介绍基本类型的包装类,主要包括基本类型的包装类使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

基本类型的包装类

基本类型不是对象,所以Java针对基本类型提供了对应的包装类,以对象的形式来使用

byte->Byte|short->Short|int->Integet|long->Long|char->Character|double->Double|boolean->Boolean

装箱 :基本类型转包装类型

拆箱 :包装类型转换成基本类型

成员方法

static 基本类型 parseXxx(String): 将字符串类型的数据转换成对应的基本类型

Character没有parseXxx方法,因为字符串想转换成char类型的数据,可以通过String类中的toCharArray方法,charAt()

package com.wang.basetype;
public class Demo01 {
    public static void main(String[] args) {
        //将基本类型转换为引用类型
        Integer i1=new Integer(30);
        //将引用类型转换为基本类型
        int b=i1.intValue();
        System.out.println(b);
        System.out.println("============================");
        //自动拆装箱
        Integer i2=20;
        int c=i2;
        //static 基本类型 parseXxx(String): 将字符串类型的数据转换成对应的基本类型
        //将字符串类型的10转换成int类型的10
        String s="20";
        int i=Integer.parseInt(s);
        System.out.println("i="+i);
        System.out.println("i+2="+(i+2));
        byte b1=Byte.parseByte(s);
        System.out.println("b1:"+b1);
        double d=Double.parseDouble(s);
        System.out.println("d:"+d);
        long l=Long.parseLong(s);
        System.out.println("l+70="+(l+70));
        boolean b2=Boolean.parseBoolean(s);
        System.out.println(b2);
    }
}

原文地址:https://www.cnblogs.com/wyj96/p/11772071.html