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?
This question is related to
java
string
integer
decimal-point
Integer.valueOf()
returns an Integer object, while Integer.parseInt()
returns an int
primitive.
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);
The difference between these two methods is:
parseXxx()
returns the primitive typevalueOf()
returns a wrapper object reference of the type.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.
Source: Stackoverflow.com