Exception in thread "main" java.lang.NumberFormatException: For input string: "9000000000000000" under radix 16

9000000000000000 base 16 is a positive number since there is no sign. Since a long is signed, the greatest number that it can hold is 0x7FFF_FFFF_FFFF_FFFF. So yours is too great.

If you want -8,070,450,532,247,928,832, use parseUnsignedLong():

        System.out.println(Long.parseUnsignedLong("9000000000000000", 16));

Output:

-8070450532247928832

Now values up to 0xFFFF_FFFF_FFFF_FFFF are accepted.


Referring to Long#parseLong(String,int)

An exception of type NumberFormatException is thrown if any of the following situations occurs:

  • The first argument is null or is a string of length zero.
  • The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
  • Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002d') or plus sign '+' ('\u002B') provided that the string is longer than length 1.
  • The value represented by the string is not a value of type long.

Examples:
  parseLong("0", 10) returns 0L
  parseLong("473", 10) returns 473L
  parseLong("+42", 10) returns 42L
  parseLong("-0", 10) returns 0L
  parseLong("-FF", 16) returns -255L
  parseLong("1100110", 2) returns 102L
  parseLong("99", 8) throws a NumberFormatException
  parseLong("Hazelnut", 10) throws a NumberFormatException
  parseLong("Hazelnut", 36) returns 1356099454469L

The decimal value parsed using radix 16 is 10376293541461622784 which is greater than Long.MAX_VALUE(9223372036854775807), violate following condition:

The value represented by the string is not a value of type long

hence throwing NumberFormatException.

import java.math.BigInteger;

public class ParseLong {
    public static void main(String[] args) {
        System.out.println("Max Long value :" + Long.MAX_VALUE);
        System.out.println("Min Long value :" + Long.MIN_VALUE);
        System.out.println("Value without overflow " + new BigInteger("9000000000000000", 16));
        System.out.println("Value by parseUnsigned " + Long.parseUnsignedLong("9000000000000000", 16));
        System.out.println("Value by literal " + 0x9000000000000000L);
    }
}