java 基本类型、包装类、字符串之间的转换

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);

原文地址:https://www.cnblogs.com/television/p/8426087.html