Integer包装类

基本数据类型的包装类可以使用基本类型对应的类的相关成员变量和成员方法,相比于基本数据类型,包装类更易于使用
八大基本数据类型
byte short int long float double char boolean
对应的包装类
Byte Short Integer Long Float Double Character Boolean
Integer类的常用方法
构造方法:
public Integer(int value)
public Integer (String s)
成员方法:

/*
 *       Integer num = new Integer(100);
 *       public int intValue()  拆箱,将Integer转化为基本类型
 *          num.intValue()       100
 *
 *       public double doubleValue()  拆箱,将Integer转化为基本类型
 *          num.doubleValue()       100.0
 *
 *       public static int parseInt(String s)  将字符串转换为int类型
 *          num.parseInt("100")     100
 *  
 *       public static String toString(int i)  将整型转换为字符串
 *          num.toString(100)       100
 *  
 *       public static Integer valueOf(int i)  将整型装换为Integer类型
 *          Integer num =new Integer(100)       装箱
 *          Integer num = Integer.valueOf(100)
 * 
 *       public static Integer valueOf(String s)  将字符串转换为Integer类型
 *          Integer num =new Integer("100")     装箱
 *          Integer num = Integer.valueOf("100");
 *          
 */

int转String的方法:
int i = 100;
方式一:String str = i + “”;
方式二:String str = String.valueOf(i);
方式三:String str = Integer.valueOf(i).toString;

Stirng转int的方法:
String str = “100”;
方式一:int i = Integer.parseInt(str);
方式二:int i = new Integer(str).parseInt();
方式三:int i = Integer.valueOf(str).intValue();

new Integer(i)和Integer.valueOf(i)的区别
Integer.valueOf(i)是在一个缓存中取值,如果传进来的值 i 在缓存(-128~127)内没有,Integer.valueOf(i)方法就会new Integer(i)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值