开发者

Error with Long datatype in java

开发者 https://www.devze.com 2023-03-30 16:01 出处:网络
Actually my system gives Long.MAX_VALUE as 9223372036854775807 But when I write my program like this, package hex;

Actually my system gives Long.MAX_VALUE as 9223372036854775807

But when I write my program like this,

package hex;

/**
 *
 * @author Ravi
 */
public class Main {

    /**
     * @p开发者_如何学编程aram args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        long x = 9223372036854775807;

        System.out.println(x);

    }

}

I am getting compile time error. Can anyone explain the reason?


With no suffix, it's an int constant (and it overflows), not a long constant. Stick an L on the end.


You might want to try using like this:

long x = 9223372036854775807L;

Without the L at the end, you'll be declaring an int.


9223372036854775807 is an int literal, and it's too big to fit in an int.
The fact that yo assign the int literal to a long makes no difference.

You need to create a long literal using the L suffix.


if you don't specify what kind of number literal is, then int is assumed. You need to specify that you want long by adding "l" or "L" (better, because "l" looks like 1) at the end of the number:

long x = 9223372036854775807L;

instead of:

long x = 9223372036854775807;
0

精彩评论

暂无评论...
验证码 换一张
取 消