string转int
//方法一:/直接使用静态方法,不会产生多余的对象,但会抛出异常
String str = "123";
try {
int a = Integer.parseInt(str);
} catch (NumberFormatException e) {
e.printStackTrace();
}
//方法二:Integer.valueOf(s) 相当于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象
String str = "123";
try {
int b = Integer.valueOf(str).intValue()
} catch (NumberFormatException e) {
e.printStackTrace();
}
int转String
//第一种方法:会产生两个String对象
s=i+"";
//第二种方法:直接使用String类的静态方法,只产生一个对象
s=String.valueOf(i);
//采用tostring方法
String s = Integer.toString(i);