java中包装类

针对八种基本数据类型定义相应的引用类型—包装类(封装类):

基本数据类型包装类父类
byteByteNumber
shortShortNumber
intIntegerNumber
longLongNumber
floatFloatNumber
doubleDoubleNumber
booleanBoolean
charCharacter

基本数据类型、包装类、String之间的相互转换

一、装箱(基本数据类型->包装类)

  1. 通过构造器
  2. 通过字符串参数
  3. 自动装箱
public void test1(){
        int num1=10;
        //System.out.println(num1.toString());  基本数据类型没有toString方法
        //通过构造器装箱
        Integer in1=new Integer(num1);
        System.out.println(in1.toString());  //Integer重写了toString方法
        //通过字符串装箱
        Integer in2=new Integer("123");
        System.out.println(in2.toString());

        //Integer in3=new Integer("123abc");
        //System.out.println(in3.toString());  报异常NumberFormatException

        Float f1=new Float(12.3f);
        Float f2=new Float("12.3f");
        System.out.println(f1.toString());//12.3
        System.out.println(f2.toString());//12.3

        Boolean b1=new Boolean(true);
        Boolean b2=new Boolean("TrUe");  //忽略大小写
        Boolean b3=new Boolean("true123");//除了true之外的都是false
        System.out.println(b1);//true
        System.out.println(b2);//true
        System.out.println(b3);//false

        Order order=new Order();
        System.out.println(order.isBoy); //false
        System.out.println(order.isGirl);//null
    }

class Order{
    boolean isBoy;  //默认值为false
    Boolean isGirl; //默认值为null
}

二、拆箱(包装类->基本数据类型)

  1. 调用包装类的方法:xxxValue()方法
  2. 自动拆箱
public void test2(){
        Integer in1=new Integer(2);
        int int1=in1.intValue();//拆箱
        System.out.println(int1+1); //3  基本数据类型能够进行四则运算

        Float f1=new Float(12.3);
        float f2=f1.floatValue();//拆箱
        System.out.println(f2+1);
    }

三、自动装箱与自动拆箱

这是jdk5.0之后的新特性,使用时注意类型匹配

public void test3(){
        int num1=10;
        Integer in1=num1;  //自动装箱:基本数据类型->包装类
        System.out.println(in1.toString());//10
        int num2=in1;  //自动拆箱:包装类->基本数据类型
 }

四、基本数据类型、包装类->String

  1. 做连接运算
  2. 调用String类重载的valueOf()方法
public void test4(){
        //方式一:做连接运算
        int num1=10;
        String str1=num1+"";
        System.out.println(str1); //10
        //方式二:调用String类重载的valueOf()方法
        float f1=12.3f;
        String str2=String.valueOf(f1);
        System.out.println(str2);//12.3
        Double d1=new Double(12.4);
        String str3=String.valueOf(d1);  
        System.out.println(str3);//12.4
    }

五、String->基本数据类型、包装类

  1. 调用包装类的parseXXX()静态方法。
public void test5(){
        //方式一:调用包装类的parseXXX()静态方法
        String str1="123";
        int num1=Integer.parseInt(str1);
        System.out.println(num1+1);//124

        String str2="123a";
        //int num2=Integer.parseInt(str2);  NumberFormatException异常

        String str3="true1";
        boolean b1=Boolean.parseBoolean(str3);
        System.out.println(b1);//false
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值