Java中包装类(Wrapper)的使用

举报
YuShiwen 发表于 2022/03/30 23:31:57 2022/03/30
1k+ 0 0
【摘要】 一.8种基本数据类型对应的包装类 针对八种基本数据类型定义相应的引用类型——包装类(封装类)有了类的特点,就可以调用类中的方法以下是8种基本数据类型对应的其包装类: 基本数据类型包装类byteByte...

一.8种基本数据类型对应的包装类

  • 针对八种基本数据类型定义相应的引用类型——包装类(封装类)
  • 有了类的特点,就可以调用类中的方法
  • 以下是8种基本数据类型对应的其包装类:
基本数据类型 包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

二.包装类与基本数据类型之间的转换(拆箱与装箱、自动拆装箱)

1.基本数据类型包装成包装类的实例 —— 装箱

基本数据类型 —>包装类:调用包装类的构造器

@Test
//基本数据类型 --->包装类:调用包装类的构造器
public void test0(){
    //通过包装类的构造器实现
    int i = 300;
    Integer t = new Integer(i);
    System.out.println(i);

    //通过字符串参数 构造 包装类对象
    Float f0 = new Float(3.88);
    Float f1 = new Float(3.88f);
    Float f2 = new Float("3.88");
    System.out.println(f0);
    System.out.println(f1);
    System.out.println(f2);

    Boolean b0 = new Boolean(true);
    Boolean b1 = new Boolean("TrUe");
    Boolean b2 = new Boolean("ture333");
    System.out.println(b0);
    System.out.println(b1);
    System.out.println(b2);

}

  
 

输出结果:

300
3.88
3.88
3.88
true
true
false

Process finished with exit code 0
  
 

2. 获得包装类对象中包装的基本类型变量 —— 拆箱

包装类—>基本数据类型:调用包装类的.xxxValue()方法

@Test
    public void test1(){
        Integer integer = new Integer(38);
        int i = integer.intValue();
        System.out.println(i);

        Float aFloat = new Float(13.14);
        float f = aFloat.floatValue();
        System.out.println(f);

        Boolean aBoolean = new Boolean(true);
        boolean b = aBoolean.booleanValue();
        System.out.println(b);
    }

  
 

输出结果:

38
13.14
true

Process finished with exit code 0
  
 

3.自动装箱,自动拆箱

JDK5.0之后,支持自动装箱,自动拆箱。但类型必须匹配。

@Test
    public void test2(){
        //自动装箱:基本数据类型 --->包装类
        int i = 10;
        Integer integer = i;//自动装箱

        boolean b = true;
        Boolean aBoolean = b;//自动装箱

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

  
 

三.基本数据类型、包装类与String之间的转换

1.基本数据类型、包装类—>String类型

  • 连接运算
  • 调用String重载的valueOf(Xxx xxx)
    @Test
    public void test3(){
        int num1 = 3;
        Byte num2 = new Byte("12");
        //连接运算
        String str1 = num1 + "";
        String str2 = num2 + "";
        //调用String重载的valueOf(Xxx xxx)
        String str3 = String.valueOf(num1);
        String str4 = String.valueOf(num2);
    }

  
 

2. String类型 —>基本数据类型、包装类

  • 调用包装类的parseXxx(String s)
@Test
public void test4(){
    String str1 = "123";
    int num1 = Integer.parseInt(str1);
    System.out.println(num1);

    String str2 = "true123";
    String str3 = "TrUE";
    boolean b1 = Boolean.parseBoolean(str2);
    boolean b2 = Boolean.parseBoolean(str3);
    System.out.println(b1);
    System.out.println(b2);
}

  
 

输出结果:

123
false
true

Process finished with exit code 0
  
 

文章来源: blog.csdn.net,作者:Mr.Yushiwen,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/MrYushiwen/article/details/109736991

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。