【Java】基本数据类型与封装类、字符串的相互转换

一、基本数据类型与其对应的封装类之间的相互转换

基本数据类型转换为封装类

1·直接赋值  (自动打包)

Integer i=3;  

2·使用封装类的含参构造函数(实例化)

Integer i=new Integer(3);  

3·使用封装类提供的valueOf()方法(静态方法)

Integer i=Integer.valueOf(3);  

 

封装类转换为基本数据类型

1·直接赋值(自动解包)

Integer i=new Integer(2);  

int j=i;  

2·使用封装类提供的value()方法

Integer i=new Integer(3);  

int j=i.intValue();  

float f=i.floatValue();  

 

二、基本数据类型与字符串之间的相互转换       

基本数据类型转换为字符串

1·使用String类的valueOf()方法(静态方法)

String s=String.valueOf(123);  

2·使用封装类的toString()方法(静态方法)

String s=Integer.toString(3);  

3·使用字符串连接符“+”

String s=""+123;  

 

字符串转换为基本数据类型

1·使用封装类的静态方法

int i=Integer.parseInt("12");  

 

三、封装类与字符串之间的相互转换

字符串转换为封装类          

1·使用封装类的含参构造函数(实例化)

Integer i=new Integer("123");  

2·使用封装类的valueOf()方法

Integer i=Integer.valueOf("123");  

 

封装类转换为字符串

1·使用字符串连接符“+”

Integer i=new Integer(123);  

String s=""+i;  

2·使用封装类的toString()方法

Integer i=new Integer(123);  

String s=i.toString();  

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值