JAVA中int、String的类型转换

int->String

int n= 12345;
String s= “”;
第一种:s += n; //会产生两个String对象
第二种:s += String.valueOf(n); //直接使用String类的静态方法,只产生一个对象

String->int

int n = 0;
Sting s = “123456”;
n = Integer.parseInt(s); //直接使用静态方法,不会产生多余的对象,但会抛出异常
n = Integer.valueOf(s).intValue(); //Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

字符串 String ->整数 int

A. 有两个方法:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

注: 字串转成 Double, Float, Long 的方法大同小异.

整数 int ->字串 String

A. 有叁种方法:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = “” + i;

猜你喜欢

转载自blog.csdn.net/ty13438189519/article/details/51942882