64bit Integer has the same limit as 32bit integer

This is just a short question but I'm still curious. When I initialize a int variable with the highest value 2147483647 it's allright. but when I want to initialize a long and assign it to it's max value, it gives me a "number too large" error

int i = 2147483647;
long j = 9223372036854775807;

1 answer

  • answered 2018-07-20 20:38 Mureinik

    9223372036854775807 is an int literal, but is too large to fit into an int, hence the error. You can use a long literal instead by adding L to the end of it:

    long j = 9223372036854775807L;
    // Here --------------------^