JAVA SE 包装类

简介

包装类:让基本数据类型拥有对象的特性。

基本数据类型与其包装类

转换方法

Integer类_构造方法

 

  • Integer (int value)  //创建一个Integer对象,表示指定的int值
  • Integer (String s) //创建一个Integer对象,表示String参数所指示的int值

Integer类_常用方法

int score1 = 20;

// 创建Integer包装类对象,表示变量score1的值

Integer score2 = new Integer(score1);

// 将Integer包装类转换为double类型

double score3 = score2.doubleValue();

// 将Integer包装类转换为float类型

float score4=score2.floatValue();

// 将Integer包装类转换为int类型

int score5 =score2.intValue();

 

自动装箱和拆箱(JDK1.5)

装箱:基本类型转换为包装类

拆箱:包装类转换为基本类型

 

//装箱

int i = 10;

Integer x = new Integer(i);//手动装箱

Integer y = i;//自动装箱

//拆箱

Double d = new Double(10.0);

double m = d.doubleValue();//手动拆箱

double n = d;//自动拆箱

基本类型和字符串的转换

基本类型转字符串

  • 使用包装类的toString()方法
  • 使用String类的valueOf()方法
  • 空字符串+基本类型
int n = 10;

String str1 = Integer.toString(n);

String str2 = String.valueOf(n);

String str3 = n + "";

字符串转基本类型

  • 使用包装类的parseXxx方法
  • 使用包装类的valueOf()方法,转换为基本类型的包装类,自动拆箱
String str = "10";

int n = Integer.parseInt(str);

int m = Integer.valueOf(str);

注意事项

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值