Java中int和String类型之间转换

int –> String对象

int i=123;字符串

String s="";方法

第一种方法:s=i+""; //会产生两个String对象异常

第二种方法:s=String.valueOf(i);  //直接使用String类的静态方法,只产生一个对象字符

第三种方法:Integer.toString(i);new

String –> int

s="123";

int i;

第一种方法:i=Integer.parseInt(s);  //直接使用静态方法,不会产生多余的对象,但会抛出异常

第二种方法:i=Integer.valueOf(s).intValue();  //Integer.valueOf(s) 至关于 new Integer(Integer.parseInt(s)),也会抛异常,但会多产生一个对象

【Double, Float, Long ,Byte, Short与字符串之间的转换方法大同小异】

相关文章
相关标签/搜索