JAVA 基本数据类型 与包装类的自动拆装箱和基础类型与String类型的转换详情
发布日期:2021-06-30 15:37:23 浏览次数:2 分类:技术文章

本文共 861 字,大约阅读时间需要 2 分钟。

package com.day25.test;

import org.junit.Test;

public class day25Test {

    public void mothod(Object object) {

        System.out.println(object);
    }
    
    //单元测试
    @Test
    public void test3(){
        //新特性
        // jdk 5.0 的新特性  自动装箱 自动拆箱
        //基本数据类型  --> 包装对象
        int num =10;
        mothod(num);
        
        //自动装箱
        Integer int1=num;  // 会自动把基本数据变成对象
        
        //自动拆箱
        Integer integer =10;
        int int2=integer;  //自动拆箱
        
    }
    
    @Test
    public void test4() {
        //包装类 /基本数据类型--->String类型
        int num3=10;
        //方式一 链接运算
        String string=num3+"";
        
        //方式二
        float f1=12.3f;
        String string2=String.valueOf(f1);
        System.out.println(string2);
        
        //String类型 ---> 基本数据类型 或 包装类
        String string3="123";
  //使用强转的话 编译时不通过了 只有有子父类关系 才能使用强转
    //    int num1=(int)string3;
    //    Integer integer= (Integer)string3;
        int int3=Integer.parseInt(string3);
        //这个字符串就可以来运算了
        System.out.println(int3+1);
        
        String boolString ="true";
        boolean bool=Boolean.parseBoolean(boolString);
        System.out.println(bool);
    }

}

 

转载地址:https://jsonll.blog.csdn.net/article/details/108113206 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java 单例模式 final关键字详解
下一篇:java jUnit 单元测试方法与包装类的详解

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月15日 11时26分39秒