简体   繁体   English

为什么我会收到“整数太大”错误? 我在最后加上一个“L”

[英]Why am I getting the error "integer number too large"? I'm including an "L" at the end

The following code is not compiling in Java:以下代码未在 Java 中编译:

java version "1.6.0_24" OpenJDK Runtime Environment (IcedTea6 1.11.1) (suse-3.1-x86_64) OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode) java 版本“1.6.0_24”OpenJDK 运行时环境 (IcedTea6 1.11.1) (suse-3.1-x86_64) OpenJDK 64 位服务器 VM(build 20.0-b12,混合模式)

public class XOR
{
    public static void main(String[] args)
    {
        long one = 595082963178094600000L;
    }
}

This throws the error:这会引发错误:

XOR.java:5: integer number too large: 595082963178094600000

But I've properly indicated it as a long: The following also throws an error:但我已经正确地指出它很长:下面也会抛出一个错误:

public class XOR
{
    public static void main(String[] args)
    {
        long one = new Long( "595082963178094600000" );
    }
}

This throws:这抛出:

java.lang.NumberFormatException: For input string: "595082963178094600000"

What am I doing wrong?我究竟做错了什么?

Well, maybe because it is too large ?好吧,也许是因为too large

595082963178094600000  //your value
  9223372036854775807  //Long.MAX_VALUE

You will need either BigInteger or BigDecimal :您将需要BigIntegerBigDecimal

new BigInteger("595082963178094600000")

The values for a long must be be between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 inclusive. long 的值必须介于 -9,223,372,036,854,775,808 和 9,223,372,036,854,775,807 之间(含)。 You cannot assign a value larger than these to a variable that's a long, even if you append an L to it, it will overflow the value and cause an error at compile time.您不能将大于这些的值分配给一个 long 变量,即使您给它 append 一个 L,它也会溢出该值并在编译时导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM