user3799365
user3799365

Reputation: 1087

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

Upvotes: 3

Views: 6727

Answers (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.

Upvotes: 1

rgettman
rgettman

Reputation: 178263

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.

Upvotes: 4

Related Questions