Integer.valueOf()与Integer.parseInt() [英] Integer.valueOf() vs. Integer.parseInt()

查看:128
本文介绍了Integer.valueOf()与Integer.parseInt()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 Integer.parseInt()处理减号(如文件所述), Integer.valueOf()<之间是否存在任何其他差异/ code>和 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.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆