Java long number太大错误?

标签 java

为什么我得到一个 int number is too large where 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 中的所有字面量数字默认为 ints,范围为 -21474836482147483647

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

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

注意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

Java Language Specification一样的

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).

关于Java long number太大错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8924896/

相关文章:

java - Object<String> 在 Java 中代表什么?

java - 无法将 SHA256 与来自 Java on .Net c# 的 RSA 签名进行匹配

java - toString() 方法打印额外的 3 个字符?

java - 使用 BeanUtils 复制属性时的类型转换?

java - 可以从 Java Servlet API 发送到相对路径的重定向吗?

java - 如何使用 Java 的 T3 协议(protocol)(weblogic)部署 War 文件?

java - 单击 JavaFX 时显示警报

java - Jetty 在多个 TCP 端口上发布端点

java - 了解 servlet-api 中的请求对象。是单例吗?

java - java.time 无法解析秒的小数部分吗?