Literal xyz of type int out of range

I work with data types at the moment in Java, and, if I understand correctly, the long type takes a value between the ranges: -9,223,372,036,854 - +9,223,372,036,854,775,807. Now, as you can see below, I created a long variable called testLong , although when I insert 9223372036854775807 into it, I get an error message:

"The literal 9223372036854775807 of type int is out of range.

I do not know why this refers to the long data type as int

Does anyone have any idea?

Code below:

 char testChar = 01; byte testByte = -128; int testInt = -2147483648; short testShort = -32768; long testLong = 9223372036854775807; float testFoat; double testDouble = 4.940656458412; boolean testBool = true; 
+64
java long-integer int
Aug 17 '11 at 12:55
source share
3 answers

Add capital L to the end:

 long value = 9223372036854775807L; 

Otherwise, the compiler will try to parse the literal as int , hence the error message

+147
Aug 17 '11 at 12:57
source share
β€” -

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.

+38
Aug 17 2018-11-11T00:
source share

Try to make 9223372036854775807L

+11
Aug 17 '11 at 12:57
source share



All Articles