java字符串与基本数据的相互转化,java随记1

java字符串与基本数据的相互转化,随记1

java.lang包中的Integer类调用其类方法public static int parseInt(String s )可以将由“数字”字符组成的字符序列,如“879”,转化为int类型,例如:

int x;
String s = "879";
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(long n)
public static String valueOf(float n)
public static String valueOf(double n)

将形如123、1569等数值转化为String对象,例如:

String str = String.valueOf(123456.2563);
发布了25 篇原创文章 · 获赞 5 · 访问量 1437

猜你喜欢

转载自blog.csdn.net/weixin_42245375/article/details/102333082