public class Primitive2String
{
public static void main(String[] args)
{
String intStr = "123";
//把一个特定字符串转换成int变量
int it = Integer.parseInt(intStr);
System.out.println(it);
String floatStr = "4.56";
//把一个特定字符串转换成float变量
float ft = Float.parseFloat(floatStr);
System.out.println(ft);
//把一个float变量转换成String变量
String ftStr = String.valueOf(2.345f);
System.out.println(ftStr);
//把一个double变量转换成String变量
String dbStr = String.valueOf(3.344);
System.out.println(dbStr);
//把一个boolean变量转换成String变量
String boolStr = String.valueOf(true);
System.out.println(boolStr.toUpperCase());
}
}
此外,在API上面找到一些,可以参考一下
string->byte
Byte static byte parseByte(String s)
byte->string
Byte static String toString(byte b)
char->string
Character static String to String (char c)
string->Short
Short static Short parseShort(String s)
Short->String
Short static String toString(Short s)
String->Integer
Integer static int parseInt(String s)
Integer->String
Integer static String tostring(int i)
String->Long
Long static long parseLong(String s)
Long->String
Long static String toString(Long i)
String->Float
Float static float parseFloat(String s)
Float->String
Float static String toString(float f)
String->Double
Double static double parseDouble(String s)
Double->String
Double static String toString(Double)