Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Long parselong throwing Number Format exception

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

like image 353
user3799365 Avatar asked Nov 26 '25 03:11

user3799365


2 Answers

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.

like image 138
rgettman Avatar answered Nov 27 '25 18:11

rgettman


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.

like image 26
Ḟḹáḿíṅḡ Ⱬỏḿƀíé Avatar answered Nov 27 '25 16:11

Ḟḹáḿíṅḡ Ⱬỏḿƀíé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!