【问题标题】:Why am I getting the error "integer number too large"? I'm including an "L" at the end为什么我收到错误“整数太大”?我在最后加上一个“L”
【发布时间】:2012-04-19 11:57:05
【问题描述】:

以下代码未在 Java 中编译:

java 版本“1.6.0_24” OpenJDK 运行时环境 (IcedTea6 1.11.1) (suse-3.1-x86_64) OpenJDK 64 位服务器虚拟机(build 20.0-b12,混合模式)

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

这会引发错误:

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

但我已经正确地指出它很长!以下也抛出错误:

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

这会抛出:

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

我做错了什么?

【问题讨论】:

  • 你试过没有引号的第二个吗?
  • 你确定这个数字没有超出多头的范围吗?如果我记得最大多头约为 9 quintillion,小于您的 595,082,963,178,094,600,000
  • 这个数字看起来很大!我希望你在数钱!

标签: java javac long-integer


【解决方案1】:

嗯,也许是因为它 too large

595082963178094600000  //your value
  9223372036854775807  //Long.MAX_VALUE

您将需要BigIntegerBigDecimal

new BigInteger("595082963178094600000")

【讨论】:

  • 谢谢。我被甩了,因为错误并没有说 long 太大,但是 integer 太大了。
【解决方案2】:

long 的值必须介于 -9,223,372,036,854,775,808 和 9,223,372,036,854,775,807 之间。您不能将大于这些值的值分配给很长的变量,即使您将 L 附加到它,它也会溢出该值并在编译时导致错误。

【讨论】:

    猜你喜欢
    • 2020-11-18
    • 2021-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 2019-04-02
    相关资源
    最近更新 更多