今天在写项目时,将String类型转换为Integer类型爆出此异常,记录如下:
代码如下:
1 String a = "2222222222"; //10个2
Integer b = Integer.valueOf(a); //把String类型转换为Integer类型
异常如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAABGCAIAAADQPymwAAAWs0lEQVR4nO2dTZLjIAyFfS4OxD18A18g1+AUqcrKK1e2yS7bXjILbJBAwnbi/NjzvkpNTTtECNnd/VALaDwAAAAAAADg52m+7QAAAAAAAABgHgh3AAAAAAAAdkAh3O83Y6/ugx4Mrm/subFn4/4+1qk7pR6jA017e8pO3903cKlrz6Mb8XV6bGD3KVJM4mt9cN6BO330OfHee9P4hr/sG74/bOO7YXuzX+fz43J2vEed8Q3vPd7KzKVDXq/EAQAAwG75lYz74PoPCzJ36k07TVHuN/OsMO3abYS7975ryZTpfjOfEu6s3wl2R16Iz1b+fBNj0/+H7i3C/a1Q/3/BzlsZulGnBuUa71WaQrj/4roWBwAAAHsmCXea8WUa+nItr4eM9XglNgjaTmo/iyDcJTshE2xP8S2q8B52vNh37jrbuzv1nbuOWe1JmI72L2mMxv35+83Yc9P2NvQ4OjZ23bV9d7mZ2HUU8fd4kaWrQ5yN+5uy2mkIQbCWoSC3hjYu7FT9lO8LddKe49jJHXlYKtmn9vYSDaYhx6eiaW9pPiPFoWvTbWJGqv5oD1UZn+pz4keJ0yyWoUGwdkbNdFJTZsp9hkznbC9jM54TDRetze2bxjfGd/G68QNpHwZpibWg3uhriYCL4zLGm27GjjbeVePyfropZIDpg2vuV3DVhZTzFJ+MzshxONL1JXEAAACwN6SM++XKhfuDisVC2I0fSXUdSvs6knBX+6X6LH4qtbnfzII5gzv13X38l2aUB9cnn2Mo7jeT1Ooor0OzUYMm3UkE/aRoB9ezUpMgead5TuwuClDqfNeeWZzrdnQ/K/dFy7jLRTL3m7FT0IiwdqdU2zO4PsZEi0PXEj3NC7QqGXdxViPGR3tOgrMrhbtUimDIl86OAjd+2TTjFWcXaeVyVtAZJoLju4Z4PnSs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aQhtsvt9fkyV1xO8hpYeh5zExnSzYvxaLVMOJWKn0hJSimzWrly/akQFwoWVks3MkQGtvbU5+ZolJ7QXu53+S/vBh32aJej8WpAAAAdsOBhPvmfGtnxg35tSH8mj9fQdoO8jOs2nTyp4y/F2wHCQAAYCdAuGeQjOx+k3Dmx4bwa/58HfPRwgwlM70Js1u4/DxItwMAANgPEO4AAAAAAADsAAh3AL4G35ARr0+/vn3/AQAAgHVAuAMAAAAAALADINwBAAAAAADYARDuAAAAAAAA7AAIdwAAAAAAAHbAP07Ajcou5IvlAAAAAElFTkSuQmCC" alt="" />
原因&解决办法:Integer在32位的编译环境下,存储长度为32位。即-2^31~2^31-1; 即-2,147,483,648~2,147,483,647,此时将Integer换成BigInteger类型,问题解决
代码如下:
String a = "2222222222"; //同样是10个2
BigInteger b = new BigInteger(a); //String 转换为 BigInteger
重要的事情说三遍:
欢迎指出错误和不对的地方,以及提出建议,我会及时更新在博客里!
欢迎指出错误和不对的地方,以及提出建议,我会及时更新在博客里!
欢迎指出错误和不对的地方,以及提出建议,我会及时更新在博客里!