Java Long parselong throwing Number Format exception

蓝咒 提交于 2019-12-11 02:44:56

问题


I have a simple code first I use this

Long.parseLong(4250120140405520712)

And it works but when I do this

Long.parseLong(42501201404055207123)

It fails. Adding an extra digit makes it throw a Number Format Exception. Can someone please explain


回答1:


Assuming you're parsing Strings into longs:

The first one works because the number 4250120140405520712 (19 digits) is less than the maximum possible long value, Long.MAX_VALUE, 9223372036854775807L.

The second one fails because it's 20 digits long, bigger than 9223372036854775807L.




回答2:


long variables can only hold a maximum of 9223372036854775807 see here for more information.
The reason the second one doesn't work is that you have exceeded the limit so the number format exception is shown.



来源:https://stackoverflow.com/questions/25922439/java-long-parselong-throwing-number-format-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!