187

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?

Neeme Praks
  • 8,150
  • 5
  • 40
  • 46
ateiob
  • 8,618
  • 10
  • 40
  • 55

5 Answers5

238

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(",", ""));
nyg
  • 1,948
  • 1
  • 21
  • 37
corsiKa
  • 76,904
  • 22
  • 148
  • 194
  • 1
    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. – anon58192932 Sep 28 '15 at 20:08
  • Using fomat for value: million.replace(/,/g, ""); – Dang Thinh Feb 07 '17 at 08:17
30

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);
Community
  • 1
  • 1
Martijn Courteaux
  • 63,780
  • 43
  • 187
  • 279
  • 1
    I believe he means to parse `"1,000,000" --> (int)1000000`, not the European decimal. – corsiKa Sep 08 '11 at 22:01
  • @glowcoder: I don't think... But I'm not sure. He wrote "decimal separator". Wiki sais following: _The decimal separator is a symbol used to mark the border between the integral and the fractional parts of a decimal numeral._ – Martijn Courteaux Sep 08 '11 at 22:02
  • 1
    Why would he want to parse an **integer** if it had a decimal? :-) – corsiKa Sep 08 '11 at 22:04
  • @glowcoder: Oh, yeah. That's true. So, this means he doesn't know what the OP is talking about or he made a mistake when he created his question. Because, talking about "decimal separator" and "integer" at same time, is contra dictional. – Martijn Courteaux Sep 08 '11 at 22:06
  • 2
    Well, that's just how it goes when you're gathering requirements. :-) – corsiKa Sep 08 '11 at 22:06
  • @Martijn Courteaux Thanks +1 for introducing NumberFormat to me. What's the correct term for that `,` that separates every 3 digits in a European number notation? :) – ateiob Sep 08 '11 at 22:14
  • 4
    @ateiob that is called a "digit group separator" or often a "thousands separator". – corsiKa Sep 08 '11 at 22:16
  • 2
    @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 ;) – Voo Sep 08 '11 at 23:59
  • @Voo That would be why I called it a "digit group separator" first, and that it is often called a "thousands separator" (notice the often part :-)) I suppose one could argue that they aren't always digits, depending on your definition, in which case I concur that "grouping separator" is an even more generic term. – corsiKa Sep 09 '11 at 00:02
  • @glowcoder Oh it was more meant in general why "thousands operator" is a bit problematic. But I just love those little tidbits that make everything involving human culture so interestingly complex - and that seemed like a perfect occasion to teach other people a bit.. even if they weren't interested ;-) – Voo Sep 09 '11 at 00:11
  • @Voo I share your enthusiasm, and do appreciate the elaboration in the hopse that others won't make the mistake. We live in a western world, which is unfortunate. I'm lucky enough to have a lot of Indian guys on my team, and have worked with a number of foreign teammates in the past, and I absolutely love the opportunities for personal growth it provides. :-) – corsiKa Sep 09 '11 at 00:17
21

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

Joe
  • 4,562
  • 9
  • 55
  • 79
8

The difference between these two methods is:

  • parseXxx() returns the primitive type
  • valueOf() returns a wrapper object reference of the type.
Paul Roub
  • 35,100
  • 27
  • 72
  • 83
Sachindra N. Pandey
  • 1,029
  • 16
  • 15
8

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.

Tomasz Nurkiewicz
  • 311,858
  • 65
  • 665
  • 652