Yun Hao

Greenhorn

Posts: 11

posted 12 years ago

see the code below:

1. int i=Integer.valueOf(s).intValue();

2. double d=Double.valueOf(s).doubleValue();

if s is null value, run statement 1 will get NumberFormatException, it will be the same for all integer type wrapper class.

if s is null value, run statement 2 will get NullPointerException.

I don't know why they have two different exceptions. but if s is non number string, all of them will get NumberFormatException which is understandable.

1. int i=Integer.valueOf(s).intValue();

2. double d=Double.valueOf(s).doubleValue();

if s is null value, run statement 1 will get NumberFormatException, it will be the same for all integer type wrapper class.

if s is null value, run statement 2 will get NullPointerException.

I don't know why they have two different exceptions. but if s is non number string, all of them will get NumberFormatException which is understandable.

Joe Borderi

Ranch Hand

Posts: 151

posted 12 years ago

I know this may not seem very satisfying, but this is the answer: that's the way it's supposed to be.

Returns a Double object holding the double value represented by the argument string s.

Leading and trailing whitespace characters in s are ignored. The rest of s should constitute a FloatValue as described by the lexical rule: FloatValue: Signopt NaN Signopt Infinity Signopt FloatingPointLiteral where Sign and FloatingPointLiteral are as defined in �3.10.2 of the Java Language Specification. If s does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation"; this exact decimal value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value. Finally, a Double object representing this double value is returned. To interpret localized string representations of a floating-point value, use subclasses of NumberFormat. Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. The two-step sequence of conversions, string to float followed by float to double, is not equivalent to converting a string directly to double. For example, the float literal 0.1f is equal to the double value 0.10000000149011612; the float literal 0.1f represents a different numerical value than the double literal 0.1. (The numerical value 0.1 cannot be exactly represented in a binary floating-point number.)

Parameters: s - the string to be parsed.

Returns: a Double object holding the value represented by the String argument.

Throws: NumberFormatException - if the string does not contain a parsable number.

Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

The first argument is interpreted as representing a signed integer in the radix specified by the second argument, exactly as if the arguments were given to the parseInt(java.lang.String, int) method.

The result is an Integer object that represents the integer value specified by the string. In other words, this method returns an Integer object equal to the value of: new Integer(Integer.parseInt(s, radix))

Parameters: s - the string to be parsed.radix - the radix to be used in interpreting s

Returns: an Integer object holding the value represented by the string argument in the specified radix.

Throws: NumberFormatException - if the String does not contain a parsable int.

**valueOf public static Double valueOf(String s) throws NumberFormatException**Returns a Double object holding the double value represented by the argument string s.

*If s is null, then a NullPointerException is thrown.*Leading and trailing whitespace characters in s are ignored. The rest of s should constitute a FloatValue as described by the lexical rule: FloatValue: Signopt NaN Signopt Infinity Signopt FloatingPointLiteral where Sign and FloatingPointLiteral are as defined in �3.10.2 of the Java Language Specification. If s does not have the form of a FloatValue, then a NumberFormatException is thrown. Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation"; this exact decimal value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value. Finally, a Double object representing this double value is returned. To interpret localized string representations of a floating-point value, use subclasses of NumberFormat. Note that trailing format specifiers, specifiers that determine the type of a floating-point literal (1.0f is a float value; 1.0d is a double value), do not influence the results of this method. In other words, the numerical value of the input string is converted directly to the target floating-point type. The two-step sequence of conversions, string to float followed by float to double, is not equivalent to converting a string directly to double. For example, the float literal 0.1f is equal to the double value 0.10000000149011612; the float literal 0.1f represents a different numerical value than the double literal 0.1. (The numerical value 0.1 cannot be exactly represented in a binary floating-point number.)

Parameters: s - the string to be parsed.

Returns: a Double object holding the value represented by the String argument.

Throws: NumberFormatException - if the string does not contain a parsable number.

**public static Integer valueOf(String s, int radix) throws NumberFormatException**Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.

The first argument is interpreted as representing a signed integer in the radix specified by the second argument, exactly as if the arguments were given to the parseInt(java.lang.String, int) method.

The result is an Integer object that represents the integer value specified by the string. In other words, this method returns an Integer object equal to the value of: new Integer(Integer.parseInt(s, radix))

Parameters: s - the string to be parsed.radix - the radix to be used in interpreting s

Returns: an Integer object holding the value represented by the string argument in the specified radix.

Throws: NumberFormatException - if the String does not contain a parsable int.