day16 包装类(Wrapper)介绍

包装类分类

在这里插入图片描述

装箱与拆箱

jdk5之前使用手动装箱和拆箱

装箱:基本类型–>包装类型
拆箱:包装类型–>基本类型

int n = 100;
// 手动装箱
// int --> Integer 
Integer integer = new Integer(n);
Integer integer1 = Integer.valueOf(n);

// 手动拆箱
// Integer --> int
int i = integer.intValue();

jdk5之后实现自动装箱和拆箱

int n2 = 200;
// 自动装箱,底层使用的是Integer.valueOf()
Integer integer2 = n2;
// 自动拆箱,底层使用的仍然是intValue()方法
int n3 = integer2;

三元运算符和if-else比较
在这里插入图片描述

String 和 包装类的转换

  1. 包装类–>String
    加空串""

    Integer i = 1;
    String str = i+"";
    

    使用integer类的toString()方法

    Integer i = 1;
    String str = i.toString();
    

    使用String.valueOf方法

    Integer i = 1;
    String str = String.valueOf(i);
    
  2. String–>包装类
    使用Integer.parseInt()方法

    String str = "123";
    Integet i = Integer.parseInt(str);// 自动装箱
    

    调用public Integer(String s)的构造器

    String str = "123";
    Integet i = new Integer(str);
    

包装类的常用方法

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值