Integer.valueOf() 与 Integer.parseInt() [英] Integer.valueOf() vs. Integer.parseInt()
问题描述
除了 Integer.parseInt()
处理减号(如文档所述)之外,Integer.valueOf()
和 Integer.parseInt()
?
Aside from Integer.parseInt()
handling the minus sign (as documented), are there any other differences between Integer.valueOf()
and Integer.parseInt()
?
并且由于两者都不能将 ,
解析为 decimal 千位分隔符(产生 NumberFormatException
),是否有可用的 Java 方法来做到这一点?
And since neither can parse ,
as a decimal thousands separator (produces NumberFormatException
), is there an already available Java method to do that?
推荐答案
实际上,valueOf
在内部使用了 parseInt
.区别在于 parseInt
返回一个 int
原语,而 valueOf
返回一个 Integer
对象.从 Integer.class 来源考虑:
Actually, valueOf
uses parseInt
internally. The difference is parseInt
returns an int
primitive while valueOf
returns an Integer
object. Consider from the Integer.class source:
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s, 10);
}
public static Integer valueOf(String s, int radix) throws NumberFormatException {
return Integer.valueOf(parseInt(s, radix));
}
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
至于用逗号解析,我不熟悉.我会对它们进行消毒.
As for parsing with a comma, I'm not familiar with one. I would sanitize them.
int million = Integer.parseInt("1,000,000".replace(",", ""));
这篇关于Integer.valueOf() 与 Integer.parseInt()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!