Java包装类

2021-04-21  本文已影响0人  媛猿YY

包装类

image.png
package com.example.javabase.baozhuang;

public class WrapTestOne {
    public static void main(String[] args) {
//        装箱:把基本数据类型转换成包装类
//        1、自动装箱
        int t1 = 2;
        Integer t2 = t1;
//        2、收到装箱
        Integer t3 = new Integer(t1);
//        测试
        System.out.println("int类型变量t1=" + t1);
        System.out.println("Integer类型对象t2=" + t2);
        System.out.println("Integer类型对象t3=" + t3);
//   拆箱:把包装类转换成基本数据类型
//        1、自动拆箱
        int t4 = t2;
//        2、手动拆箱
        int t5 = t2.intValue();
//        测试
        System.out.println("Integer类型对象t2=" + t2);
        System.out.println("t4=" + t4);
        System.out.println("t5=" + t5);


    }
}

基本数据类型和字符串之间的转换

package com.example.javabase.baozhuang;

public class WrapTestTwo {
   public static void main(String[] args) {
//        基本数据类型转换为字符串
       int t1 = 2;
       String t2 = Integer.toString(t1);
       System.out.println(t2);
//       字符串 型转换为基本数据类
       //1、包装类的parse
       int t3 = Integer.parseInt(t2);
//        2、包装类的valueOf
       int t4 = Integer.valueOf(t2);
       System.out.println(t3);
       System.out.println(t4);
   }
}

上一篇 下一篇

猜你喜欢

热点阅读