为什么Integer.parseInt(" 11111111111111111111111111111111",2)在java中抛出异常? [英] why Integer.parseInt("11111111111111111111111111111111",2) throws exception in java?

查看:192
本文介绍了为什么Integer.parseInt(" 11111111111111111111111111111111",2)在java中抛出异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Integer.parseInt(11111111111111111111111111111111,2)抛出

why does Integer.parseInt("11111111111111111111111111111111",2) throw

java.lang.NumberFormatException: For input string: "11111111111111111111111111111111" 

在java中,整数是32位,我期望有效的返回值,出了什么问题这里?

In java integer is 32 bit, I expect a valid return value, what is going wrong here ?

推荐答案

整数真的是32位,但是一位用于正/负号。

Integer is truly 32 bit, but one bit is used for positive/negative sign.

此代码

// 31 instead of 32
System.out.println(Integer.parseInt("1111111111111111111111111111111",2)); 
System.out.println(Integer.MAX_VALUE);

将产生完全相同的数字2147483647.。

will produce exactly same number 2147483647.

编辑:
Integer.parseInt规范声明指定负值的正确方法是使用 - 减号:

/**
 * 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>'&#92;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>'&#92;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>.
 */

这篇关于为什么Integer.parseInt(&quot; 11111111111111111111111111111111&quot;,2)在java中抛出异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆