鉴于字符串s
已宣布,以下5行代码产生同样的结果:转换和为int
int i = Integer.valueOf(s);
int y = Integer.parseInt(s);
int j = Integer.valueOf(s).intValue();
Integer x = Integer.valueOf(s);
Integer k = Integer.valueOf(s).intValue();
是否有情况,其中每个人都成为首选的代码?看起来int
和Integer
是可以互换的,并且.intValue()
是不需要的。
相关:http://stackoverflow.com/questions/13933287/which-one-is-faster-integer-valueofstring-string-or-integer-parseintstring-s – assylias
你打算如何使用分配后的变量?你可能不需要Integer – Dan
'Integer z = Integer.parseInt(s);'也可以。 – assylias