I'm running this timer example, and I changed i from int to float, to test my machines potential :-) :
//measuring elapsed time using System.nanoTime
long startTime = System.nanoTime();
for(long i=0; i< 9100080000; i++){
Object obj = new Object();
}
long elapsedTime = System.nanoTime() - startTime;
But java complains that:
1 error found:
File: C:\Users\Adel\Code\Javas\MeasureTimeExampleJava.java [line: 16]
Error: integer number too large: 9100080000
It's saying "integer", meaning a long integer? But wouldn't long fit 9,100,080,000 into it? 2^63 is 9223372036854775808 . Any tips appreciated thanks!
It's not the variable which is an int - it's the literal that you're comparing it with. You just need:
i < 9100080000L
The compiler doesn't use "what you're doing with the literal" as part of the process of determining the type of the literal, nor does it change the type of the literal based on the value (unlike C#, where 9100080000 would be implicitly typed as long because it's too big for an int).
From the JLS section 3.10.1:
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).
...
The largest decimal literal of type int is 2147483648 (231).
All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear.
It is a compile-time error if a decimal literal of type int is larger than 2147483648 (231), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4).
Note that while both L and l work, I'd always recommend using L instead for clarity - otherwise it can look very much like a 1 depending on your font.
9100080000 is a regular integer constant, and it's out of range. To make it a long integer constant, so it will be in range, you can append L or l:
for(long i=0; i< 9100080000L; i++){
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With