int -> String
int i=12345;String s="";第一種方法:s=i+"";第二種方法:s=String.valueOf(i);這兩種方法有什么區別呢?作用是不是一樣的呢?是不是在任何下都能互換呢?
第三種方法:s=Integer.toString(i);
String -> int
s="12345";int i;第一種方法:i=Integer.parseInt(s);第二種方法:i=Integer.valueOf(s).intValue();這兩種方法有什么區別呢?作用是不是一樣的呢?是不是在任何下都能互換呢?
以下是答案:
第一種方法:s=i+""; 第一種方法:i=Integer.parseInt(s);//直接使用靜態方法,不會產生多余的對象,但會拋出異常第二種方法:i=Integer.valueOf(s).intValue();//Integer.valueOf(s) 相當于 new Integer(Integer.parseInt(s)),也會拋異常,但會多產生一個對象 |
新聞熱點
疑難解答