zoukankan      html  css  js  c++  java
  • 基本数据类型&包装类&String之间的相互转换

    1.包装类的种类

    2. 转换图解

    3.具体代码

    public class JunitTest {
    
        //基本数据类型 ---> 包装类 :装箱
        @Test
        public void test1() {
            //调用包装类的 构造器
            int i =10;
            Integer int1 = new Integer(i);
            
            float f1= 12.4f;
            Float fl1 = new Float(f1);
        }
        
        //包装类 ---> 基本数据类型 :拆箱
        @Test
        public void test2() {
            //调用包装类的 xxxValue() 
            Integer int1 = new Integer(12);
            int i1 = int1.intValue();
            
            Double dou1 = new Double(13.4);
            double d1= dou1.doubleValue();
        }
        
        //自动 装箱&拆箱
        @Test
        public void test3() {
            //从 JDK 5.0 开始 自动装箱&拆箱
            int i1=10;
            Integer int1 = i1;
            
            Double dou1 = new Double(12.6);
            double d1 = dou1;
            
            float f1= 11.6f;
            Float fl1 = f1;
        }
        
        //基本数据类型&包装类 ---> String : 调用String重载的valueOf();
        @Test
        public void test4() {
            //方式1: 连接运算
            int i1 = 12;
            String str2 = i1+"";
            System.out.println(str2);
            
            //方式2:String.valueOf();
            int i=10;
            String str1 = String.valueOf(i);
            System.out.println(str1);
        }
        
        //String ---> 基本数据类型&包装类 : 调用包装类的 parseXxx();
        @Test
        public void test5() {
            String str = "123";
            int a = Integer.parseInt(str);
            System.out.println(a+1);
            
            String str2 = "true";
            boolean b  = Boolean.parseBoolean(str2);
            System.out.println(b);
        }
    }

    重点 : ①.包装类型转String ---> String.valueOf();     ②.String转包装类 ---> 包装类 . parseXxx();

  • 相关阅读:
    64位windows 7下配置TortoiseGit(转)
    linux中fork函数详解(转)
    Socket通信的Python实现
    Socket
    浅谈CSRF攻击方式(转)
    Burpsuite常用模块详解以及渗透测试上的运用
    大佬内网渗透技巧的独白(思路篇)
    CTFcrackTools-V3
    厂商要知道的漏洞防护措施
    如何运用kali-xplico网络取证分析?点开看看吧
  • 原文地址:https://www.cnblogs.com/Anonymity-zhang/p/14292330.html
Copyright © 2011-2022 走看看