String convert int , and int convert String

来源:互联网 发布:linux源码 编辑:程序博客网 时间:2024/06/16 03:30

1 ,   int 2 String

int  a = 1234;

String s = "";

s = a + "";                                               //会产生两个String对象

s = String.valueOf(a);                         //直接使用String类的静态方法,只产生一个对象

 

2 ,  String 2 int

S = "1111";

int i ;

i = Integer.parseInt(S);                                // 不产生多余对象,但会抛出异常

I = Integer.valueOf(s).intValue;                 //会产生对于对象,并也会抛出异常,相当于new Integer(Integer.parseInt(S));

注意字符转化成int类型时,捕获异常。