Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java长数字太大错误?_Java - Fatal编程技术网

Java长数字太大错误?

Java长数字太大错误?,java,Java,为什么我得到的整数太大,而long被分配给min和max /* 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). Use this data type when y

为什么我得到的整数太大,而long被分配给min和max

/*
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).
Use this data type when you need a range of values wider than those provided by int.
*/
package Literals;

public class Literal_Long {
     public static void main(String[] args) {
        long a = 1;
        long b = 2;
        long min = -9223372036854775808;
        long max = 9223372036854775807;//Inclusive

        System.out.println(a);
        System.out.println(b);
        System.out.println(a + b);
        System.out.println(min);
        System.out.println(max);
    }
}

java中的所有文字数字默认为
int
,其范围为
-2147483648
2147483647

您的文字超出此范围,因此要进行此编译,您需要指出它们是
文字(即后缀为
L
):

请注意,java同时支持大写
L
和小写
L
,但我建议不要使用小写
L
,因为它看起来像
1

long min = -9223372036854775808l; // confusing: looks like the last digit is a 1
long max = 9223372036854775807l; // confusing: looks like the last digit is a 1
同样

如果整型文字以ASCII字母L或L(ell)作为后缀,则其类型为long;否则为int型(§4.2.1)


您必须使用
L
向编译器说明它是一个长文本

long min = -9223372036854775808L;
long max = 9223372036854775807L;//Inclusive

你可以用
Long.MIN\u VALUE
Long.MAX\u VALUE
1L>>1来装腔作势:你也可以用一个小的
l
@OliverWeiler,我想说,要装腔作势,你可以但不应该用一个小的l,因为它看起来像1。如果你用的字体不好,它看起来像1,但当然,为什么要冒混淆的风险呢。
long min = -9223372036854775808L;
long max = 9223372036854775807L;//Inclusive