包装类的使用

本文详细探讨了Java中八种基本数据类型的包装类,包括自动装箱拆箱机制,以及String类型与包装类的转换。通过实例演示了基本类型到包装类的转换、包装类的Value方法使用,以及String与数值类型之间的灵活转换技巧。
摘要由CSDN通过智能技术生成

  • 针对八种基本数据类型定义相应的应用类型–包装类(封装类)
基本数据类型包装类
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
booleanBoolean
charCharacter

基本数据类型,包装类和String类之间的转化:

基本数据类型,包装类和String类之间的转化

基本数据类型–》包装类

class order{
    boolean ismale;
    Boolean isfemale;
}
public class JUnitTest {

    //基本数据类型-》包装类
    @Test
    public void test1() {
        int num1 = 10;
        //System.out.println(num1.toString());//基本数据类型没有方法
        Integer in1 = new Integer(num1);
        System.out.println(in1.toString());//10

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

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

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

        Boolean b1 = new Boolean(true);
        Boolean b2 = new Boolean("true");
        Boolean b3 = new Boolean("abc123");//不报错,只要不等于true(不区分大小写)就返回false
        System.out.println(b3);

        order o = new order();
        System.out.println(o.ismale);//false
        System.out.println(o.isfemale);//null
    }
}

包装类–》基本数据类型

调用包装类的xxxValue()

//包装类--》基本数据类型,调用包装类的xxxValue()
    @Test
    public void test2(){
        Integer in1 = new Integer(12);
        int i1 = in1.intValue();
        System.out.println(i1 + 1);

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

自动装箱与自动拆箱

可以把包装类当做基本数据类型用

  @Test
    public void test3(){
        //自动装箱:基本数据类型--》包装类
        int num2 = 10;
        Integer in1 = num2;
        boolean b1 = true;
        Boolean b2 = b1;
        System.out.println(num2 + " " + in1 + " " + b1 + " " + b2);

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

基本数据类型,包装类–》String类型

@Test
    public void test4(){
        //方式一:连接运算
        int num = 10;
        String s1 = num + "**";
        System.out.println(s1);
        //方式二:调用String重载的valueOf()方法
        float f1 = 12.3f;
        String s2 = String.valueOf(f1);//"12.3"
        System.out.println(s2);

        Double d1 = new Double(14.5);
        String s3  = String.valueOf(d1);
        System.out.println(s3);
    }

String类型–》基本数据类型,包装类

调用包装类的parsexxx(String s)

@Test
    public void test5(){
        //String str1 = "123ddf";//报错
        String str1 = "123";
        int num2 = Integer.parseInt(str1);
        System.out.println(num2 + 1);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pk5515

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值