包装类的使用及注意事项

基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

ps:以上几个数值型的包装类都继承于Number。
一、基本数据类型转换为包装类
1.java提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征。
2.基本数据类型、包装类、String三者之间的相互转换。

 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 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");
       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+1);
发布了30 篇原创文章 · 获赞 0 · 访问量 81

猜你喜欢

转载自blog.csdn.net/qq_45370866/article/details/105516518