Java Long parselong throwing Number exception format

I have simple code, first I use this

Long.parseLong(4250120140405520712) 

And it works, but when I do it

 Long.parseLong(42501201404055207123) 

He is failing. Adding an additional digit causes it to generate a number format exception. Can someone explain

+2
source share
2 answers

Assuming you are parsing String into long s:

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

The second setback is because it is 20 digits longer than 9223372036854775807L .

+3
source

long variables can contain a maximum of 9223372036854775807, see here for more information.
The reason the second one doesn’t do t is because you have exceeded the limit, so a numerical exception is shown.

0
source

All Articles