包装类、基本类型和字符串之间转换1

1、基本类型和包装类 
基本类型和包装类可通过自动装箱和拆箱实现。 
int i = 24; 
Integer a = new Integer(i); //手动装箱 
Integer b = i; //自动装箱 
int x = a; //自动拆箱 
int y = a.intValue(); //受到拆箱

2、基本类型转String 
a.使用包装类的toString方法 
int i =24; 
String str1 = Integer.toString(i); 
b.使用String类的valueOf方法 
String str2 = String.valueOf(i); 
c.基本类型加空字符串 
String str3 = i + “”;

3、String转基本类型 
a.调用包装类的静态方法parseXxx,返回值为基本类型 
String str = “24”; 
int a = Integer.parseInt(str); 
b.调用包装类的静态方法valueOf,返回值为包装类 

int b = Integer.valueOf(str);

public void test1() {
		//包装类型和基本类型互转
		Integer i = new Integer(100);
		int ii=i.intValue();
		
		//包装类型和String类型互转
		Integer iii=new Integer("1000");
		String stri=iii.toString();
		
		//基本类型和String类型互转
		int i2=Integer.parseInt("1000");
		//int i3=Integer.valueOf("200");
		String sst=i2+"";
		String sst1=String.valueOf(i2);
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值