问题
why does Integer.parseInt("11111111111111111111111111111111",2) throw
java.lang.NumberFormatException: For input string: "11111111111111111111111111111111"
In java integer is 32 bit, I expect a valid return value, what is going wrong here ?
回答1:
Integer is truly 32 bit, but one bit is used for positive/negative sign.
This code
// 31 instead of 32
System.out.println(Integer.parseInt("1111111111111111111111111111111",2));
System.out.println(Integer.MAX_VALUE);
will produce exactly same number 2147483647.
EDIT:
Integer.parseInt specification states that the correct way to specify negative value is to use - minus sign:
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to
* indicate a negative value. The resulting integer value is returned.
* <p>
* An exception of type <code>NumberFormatException</code> is
* thrown if any of the following situations occurs:
* <ul>
* <li>The first argument is <code>null</code> or is a string of
* length zero.
* <li>The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.
* <li>Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
* <code>'-'</code> (<code>'\u002D'</code>) provided that the
* string is longer than length 1.
* <li>The value represented by the string is not a value of type
* <code>int</code>.
* </ul><p>
* Examples:
* <blockquote><pre>
* parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("2147483647", 10) returns 2147483647
* parseInt("-2147483648", 10) returns -2147483648
* parseInt("2147483648", 10) throws a NumberFormatException
* parseInt("99", 8) throws a NumberFormatException
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787
* </pre></blockquote>
*
* @param s the <code>String</code> containing the integer
* representation to be parsed
* @param radix the radix to be used while parsing <code>s</code>.
* @return the integer represented by the string argument in the
* specified radix.
* @exception NumberFormatException if the <code>String</code>
* does not contain a parsable <code>int</code>.
*/
回答2:
You assume that you are passing the raw bits to the method, but in fact you are passing a binary representation of the number. Therefore you can specify up to 31 bit, and a sign. This gives the result you have expected:
Integer.parseInt("-1", 2);
The contract of the method is such that you can specify any (reasonable) base, for example the hexatridecimal base:
Integer.parseInt("-1", 36);
From this it should be obvious that this is about the conversion from a representation in an arbitrary number system to an int, as opposed to passing raw contents of the int.
回答3:
The integer is too big. The maximum value is 2 147 483 647
回答4:
Integers in java are signed integer. Thus the first bit is needed for the - or + information.
As the java documentation of Integer.MAX_VALUE says:
Integer.MAX_VALUE A constant holding the maximum value an int can have, 2^31-1.
The 32 bits "11111111111111111111111111111111" would be 4294967296 and therefore be out of the range of an integer.
回答5:
Max value of int32 is 2,147,483,647
来源:https://stackoverflow.com/questions/18170101/why-integer-parseint11111111111111111111111111111111-2-throws-exception-in-j