Java 面向对象中(四)

包装类在具体使用中需要三种类型的直接转换
学习了基本类型 包装类 String类型的转换

public class WrapperTest {
    public static void main(String[] args) {
        //基本数据类型 ——> 包装类:调用包装类的构造器
        int num1 = 10;
        Integer in1 =Integer.valueOf(num1);
        System.out.println(in1.toString());

        Integer in2 = Integer.valueOf("123");
        System.out.println(in2.toString());

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

        Boolean b1 = new Boolean(true);
        Boolean b2 = new Boolean("true");
        System.out.println(b2);
        Boolean b3 = new Boolean("true123");
        System.out.println(b3);

        //包装类 ——> 基本数据类型:调用包装类的xxxValue
        Integer in3 = new Integer(12);
        int i1 = in3.intValue();
        System.out.println(i1 + 1);

        Float f3 = new Float(12.3);
        float f4 = f3.floatValue();
        System.out.println(f4);
    } }
//String类型 ————> 基本数据类型 包装类:parseXxx()
String str4 = "123";
int num5 = Integer.parseInt(str4);
System.out.println(num5 + 1);

String str5 = "true";
Boolean b6 = Boolean.parseBoolean(str5);
System.out.println(b6);
//基本数据类型 包装类 ————> String类型:调用String重载的valueOf(Xxx xxx)
int num4 = 10;
//方式1:连接运算
String str1 = num4 + "";
//方式2:调用String的valueOf(Xxx xxx)
float f5 = 12.3f;
String str2 = String.valueOf(f5);
System.out.println(f5);

Double d1 = new Double(12.4);
String str3 = String.valueOf(d1);
System.out.println(str3);

并了解到自动装箱与自动拆箱的小知识
最后补充了例题的编写

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值