在程序开发中,会经常遇到将基本数据类型转成String类型,或者将String类型转换为基本数据类型
基本数据类型转String类型: 用以下方法即可转换成功
int num1 = 100;
byte num2 = 20;
short num3 = 50;
float num4 = .231F;
double num5 = 2.33;
long num6 = 114514L;
boolean b1 = true;
String st1 = num1 + "";
String st2 = num2 + "";
String st3 = num3 + "";
String st4 = num4 + "";
String st5 = num5 + "";
String st6 = num6 + "";
String st7 = b1 + "";
String转换基本数据类型:用以下方法既可转换成功
String str = "100";
int n1 = Integer.parseInt(str);
short n2 = Short.parseShort(str);
byte n3 = Byte.parseByte(str);
float n4 = Float.parseFloat(str);
double n5 = Double.parseDouble(str);