Java long number too large error?

前端 未结 2 1249

Why do I get an int number is too large where the long is assigned to min and max?

/*
long: The long data type is a 64-bit signed two\'s complement integer.
         


        
相关标签:
2条回答
  • 2020-11-28 14:53

    All literal numbers in java are by default ints, which has range -2147483648 to 2147483647 inclusive.

    Your literals are outside this range, so to make this compile you need to indicate they're long literals (ie suffix with L):

    long min = -9223372036854775808L;
    long max = 9223372036854775807L;
    

    Note that java supports both uppercase L and lowercase l, but I recommend not using lowercase l because it looks like a 1:

    long min = -9223372036854775808l; // confusing: looks like the last digit is a 1
    long max = 9223372036854775807l; // confusing: looks like the last digit is a 1
    

    Java Language Specification for the same

    An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).

    0 讨论(0)
  • 2020-11-28 15:05

    You must use L to say to the compiler that it is a long literal.

    long min = -9223372036854775808L;
    long max = 9223372036854775807L;//Inclusive
    
    0 讨论(0)
提交回复
热议问题