ChatGPT解决这个技术问题 Extra ChatGPT

Integer.valueOf() vs. Integer.parseInt() [duplicate]

This question already has answers here: Different between parseInt() and valueOf() in java? (11 answers) Closed 4 years ago.

Aside from Integer.parseInt() handling the minus sign (as documented), are there any other differences between Integer.valueOf() and Integer.parseInt()?

And since neither can parse , as a decimal thousands separator (produces NumberFormatException), is there an already available Java method to do that?

Could we get a clarification, since you're dealing with integers... Are you trying to get "1,000,000" into an int of value 1000000? Or something like "123,4" into an int value of 123?
Care to tell us which integer has a decimal separator? --- Oh, glowcoder beat me ;)
@glowcoder Yes, getting "1,000,000" into an int of value 1000000 is exactly what I meant. Thanks!
That's a thousands separator, not a decimal separator.
@KarlKnechtel depends on where you're from, actually.

n
nyg

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(",", ""));

Thanks for this. I was using Integer.valueOf and getting a NullPointerException when pulling an int from a database and trying to convert it from String to int in Java. NOT String to Integer. I had to switch to Integer.parseInt() instead.
Using fomat for value: million.replace(/,/g, "");
C
Community

First Question: Difference between parseInt and valueOf in java?

Second Question:

NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234");
double d = number.doubleValue();

Third Question:

DecimalFormat df = new DecimalFormat();
DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator(',');
df.setDecimalFormatSymbols(symbols);
df.parse(p);

I believe he means to parse "1,000,000" --> (int)1000000, not the European decimal.
Why would he want to parse an integer if it had a decimal? :-)
Well, that's just how it goes when you're gathering requirements. :-)
@ateiob that is called a "digit group separator" or often a "thousands separator".
@glowcoder Just as an interesting tidbit I think that the name for that seperator isn't officially "thousands something" for the simple reason that there are locales out there that group numbers in other groups (ie not 3s). Read about that on Michael Kaplan's blog - iirc Indian would be one example (I think they split it in pairs of 4s?). So "Grouping Separator" makes some sense for a international audience ;)
J
Joe

Integer.valueOf() returns an Integer object, while Integer.parseInt() returns an int primitive.


P
Paul Roub

The difference between these two methods is:

parseXxx() returns the primitive type

valueOf() returns a wrapper object reference of the type.


T
Tomasz Nurkiewicz

parseInt() parses String to int while valueOf() additionally wraps this int into Integer. That's the only difference.

If you want to have full control over parsing integers, check out NumberFormat with various locales.