基本类型和封装类的转换
Int转Integer: Integer integer = new Integer(int); Integer转int: int i = integer.intValue(); Double转double: double b = Double.doubleValue(); Float转float: float c = Float.floatValue();
(1) 自动类型转换:编译器自动完成类型转换,不需要在程序中编写代码; 规则:从存储范围小的类型到存储范围大的类型。 具体规则:byte→short(char)→int→long→float→double.
2.基本数据类型与字符串之间的转换:
一. int转换成String
①.整型转换成字符型 String num = Integer.toString(int n); ②.Long型转换成字符型 String num = Long.toString(long n); ③.Short型转换成字符型 String num = Short.toString(Short n); ④.Float型转换成字符型 String num = Float.toString(Float n); ⑤.Double型转换成字符型 String num = Double.toString(Double n);
二. String转换成int
①.转换成Int型 int/Integer num = Integer.parseInt(String str); ②.转换成long型 Long/long num = Long.parseLong(String str); ③.转换成short型 short/Short num = Short.parseShort(String str); ④.转换成float型 float/Float num = Float.parseFloat(String str); ⑤.转换成double型 double/Double num = Double.parseDouble(String str);