java.lang包中的Integer类调用其类方法public static int parseInt(String s)可以将由“数字”字符组成的字符串,如"123", 转化为int型数据,例如:
int x; String s = "123"; x = Integer.parseInt(s);
除此之外,还可以使用java.lang包中的Byte、Short、Long、Float、Double类调用相应的类方法,例如:
public static byte parseByte(String s) throws NumberFormatException public static short parseShort(String s) throws NumberFormatException public static long parseLong(String s) throws NumberFormatException public static float parseFloat(String s) throws NumberFormatException public static double parseDouble(String s) throws NumberFormatException
通过上述方法,我们可以将由“数字”字符组成的字符串转化为相应的基本数据类型。
当然,我们也可以使用String类的下列类方法:
public static String valueOf(byte n) public static String valueOf(int n) public static String valueOf(short n) public static String valueOf(long n) public static String valueOf(float n) public static String valueOf(double n)
将数值转换为字符串,例如:
String str = String.valueOf(123);
本文固定URL:https://www.dotcpp.com/course/822