3

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

user3799365
  • 1,087
  • 4
  • 17
  • 27

2 Answers2

4

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

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.