Java:包装类

本篇文章解决问题

1. 包装类的使用

包装类的使用

包装类的使用:

  1. Java提供了8种基本数据类型包装类的使用,使得基本数据类型的变量具有类的特征

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

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bIX428vw-1596547364183)(F:\JianShu_material\Java\包装类\002.png)]

    // 基本数据类型转换为包装类
    // 调用包装类的构造器
    
    public class Test1 {
        public static void main(String[] args) {
            int num = 10;
    
            Integer in1 = new Integer(num);
            Integer in2 = new Integer("123");
            //Integer in3 = new Integer("asc")会报异常
            System.out.println(in1.toString());
            System.out.println(in2.toString());
    
            Float f1 = new Float(1.2f);
            Float f2 = new Float("1.2f");
            System.out.println(f2.toString());
    
            Boolean b1 = new Boolean(true);
            Boolean b2 = new Boolean("true");
            Boolean b3 = new Boolean("123true");//输出False
            
        }
    }
    
    // 包装类转换为基本数据类
    // 调用包装类Xxx的xxxValue()方法
    
    public class Test2 {
        public static void main(String[] args) {
    
            Integer in1 = new Integer(123);
            int i1 = in1.intValue();
            System.out.println(i1);
    
            Float f1 = new Float(1.23);
            float f2 = f1.floatValue();
            System.out.println(f2);
    
        }
    }
    
    // 自动装箱和自动拆箱
    
    public class Test3 {
        public static void main(String[] args) {
    
            //自动装箱:基本数据类型--->包装类
            int i1 = 123;
            Integer in1 = i1;
            System.out.println(in1.toString());
    
            //自动拆箱:包装类--->基本数据类型
            float f2 = 1.23f;
            Float f1 = f2;
            System.out.println(f1);
        }
    }
    
    // 基本数据类型、包装类----->String
    
    public class Test4 {
        public static void main(String[] args) {
    
            // 方式一:采用拼接
            int i1 = 123;
            String s1 = i1 + "";
            System.out.println(s1);
    
            // 方式二:使用String.valuOf()
            float f1 = 1.23f;
            String s2 = String.valueOf(f1);
            System.out.println(s2);
    
            Double d1 = new Double(1.22);
            String s3 = String.valueOf(d1);
            System.out.println(s3);
    
        }
    }
    
    // String----->基本数据类型、包装类
    // 调用方法parseXxx(String s)
    
    public class Test5 {
        public static void main(String[] args) {
    
            String s1 = "123";
            int i = Integer.parseInt(s1);
            System.out.println(i);
    
            String s2 = "true";
            boolean b1 = Boolean.parseBoolean(s2);
            System.out.println(b1);
    
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值