I don't know why this refers to a long data type as int
This is not true. You must learn to trust compiler messages (especially when they are from normal, modern compilers, and not with ancient C / C ++ compilers). Although the language they speak can be difficult to decipher at times, they usually don't lie to you.
Look again:
The literal int 9223372036854775807 is out of range.
Note that it does not mention your testLong variable or the long type anywhere, therefore not about initialization. The problem seems to be happening at some other point.
Now let's look at some parts of the message:
int tells us that he wants to consider something as an int value (this is not what you wanted!)- "out of range" is pretty clear: something is not within the expected range (possibly from
int ) - Literal: Now this is interesting: what is literal?
I will stay on the cozy list to talk about literals for a moment: literals are places where you have some kind of value in your code. There are String literals, int literals, class literals, etc. Each time you specify a value explicitly in your code, it is literal.
Thus, this does not mean that you are imposing a variable declaration on you, but the number itself, the value is what you need.
You can easily verify this using the same literal in a context where the same long and int are valid:
System.out.println(9223372036854775807);
PrintStream.println can accept a int or a long (or pretty much everything else). So the code has to be accurate, right?
No. Well maybe it should be, but by the rules not very good.
The problem is that "some digits" are defined as an int literal , and therefore must in the range defined by int .
If you want to write a long literal, you must make it explicit by adding L (or in lowercase L , but I'm high ) so you always use the upper -case option, because it is much easier to read and harder to make mistakes for 1 ).
Please note that a similar problem occurs with float ( F / F postfix) and double ( D / D postfix).
Note: you will understand that there are no byte or short literals, and you can still assign values ββ(usually int literals) to byte and short variables: this is possible due to special rules in Β§ 5.2 on Assignment Converson : they allow you to assign larger expressions to constant expressions of type byte , short , char or int , if the values ββare within the range of types.