包装类、基本数据类型、String之间的转换 自动拆箱与装箱

包装类

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

 

基本数据类型与Object毫无关系,我们需要让基本数据类型也有类的特征,也面向对象

这个时候我们可以将基本数据类型封装在类中,有了类的特点,就可以调用类的方法,这个时候才叫真正的面向对象。

基本数据类型---->包装类:调用包装类的构造器

public void test1(){
        //int型
        int num1 = 10;
        Integer in1 = new Integer(num1);
        System.out.println(in1.toString());

        Integer in2 = new Integer("123");
        System.out.println(in2.toString());
        //报异常
//      Integer in3 = new Integer("123abc");
//      System.out.println(in3.toString());
        //float型
        Float f1 = new Float(21.3f);
        Float f2 = new Float("21.3");
        System.out.println(f1);
        System.out.println(f2);
        //布尔型
        Boolean b1 = new Boolean("true");
        Boolean b2 = new Boolean(true);
        Boolean b3 = new Boolean("true123");
        System.out.println(b1);
        System.out.println(b3);  //false

        Orderer orderer = new Orderer();
        System.out.println(orderer.isMale);  //false 默认值
        System.out.println(orderer.isFemale);  //null
    }
}
class Orderer{
    boolean isMale;
    Boolean isFemale;
}

包装类--->基本数据类型:调用包装类的xxxValue()方法

可将包装类转换为基本数据类型

@Test
    public void test2(){
        Integer in1 = new Integer(12);
        int i1 = in1.intValue();
        System.out.println(i1 + 1); //13

        Float f1 = new Float(19.9);
        float f2 = f1.floatValue();
        System.out.println(f2);

    }

JDK 5.0新特性  自动装箱与拆箱

自动装箱:基本数据类型---->包装类的对象 可替代:调用包装类的构造器 不用new构造器了

自动拆箱 包装类----->基本数据类型

即两者可以自动转换

@Test
    public void test3(){
        int num = 10;
        //基本数据类型---->包装类的对象
        method(num);
        //自动装箱:基本数据类型---->包装类的对象  可替代:调用包装类的构造器 不用new构造器了
        int num2 = 10;
        Integer in1 = num2;//自动装箱
        boolean b1 = true;
        Boolean b2 = b1;//自动装箱

        //自动拆箱  包装类----->基本数据类型
        System.out.println(in1.toString());
        int num3 = in1;

    }
    public void method(Object obj){
        System.out.println(obj);
    }

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

调用String重载的valueOf(xxx)方法

@Test
    public void test4(){
        int num = 12;
        //方式一:连接运算
        String str = num + "";
        System.out.println(str);

        //方式二 调用String重载的valueOf(XXX XxX)
        double num2 = 12.3;
        String str2 = String.valueOf(num2);
        System.out.println(str2);

        Double num3 = 12.4;
        String str3 = String.valueOf(num3);
        System.out.println(str3);
    }

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

调用包装类的parseXxx(String s)方法

@Test
    public void test5(){
        String str1 = "123";
        //String str1 = "123a";  java.lang.NumberFormatException
        //错误的情况
        //int num1 = (int)str1;
        //Intrger num2 = (Integer)str1;

        int num2 = Integer.parseInt(str1);
        System.out.println(num2 + 1);

        String str2 = "true1";
        boolean b1 = Boolean.parseBoolean(str2);
        System.out.println(b1);

    }

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值