Assigning a Value to Long Data Type in Java - Defaulting to Int
Long Val = 5000000000; the Error During This Assignment Is: the Literal 5000000000 of Type Int Is out of Range Why Does the Compiler by Default Assume the...
long val = 5000000000;
The error during this assignment is:
The literal 5000000000 of type int is out of range
Why does the compiler by default assume the literal to be type int when it is declared with type long?
4 Answers
There is aspecific suffixes for long i.e L. If there is no suffix, then 5000000000 assumed to be an int type. And 5000000000 is out of int range, causing the erro. So you need to add L at the end of 5000000000 for it to be treated as a long value. Change your declaration from
long val = 5000000000;
to
long val = 5000000000L;
Add the letter L at the end of your number as shown below
long val = 5000000000L;
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).
You should append 'l' or 'L' to the value explicitly used to initialize the variable; even as small as 0.
long val = 0L;