Java 600851475143 的“整数太大”错误消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3757763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:29:40  来源:igfitidea点击:

"Integer number too large" error message for 600851475143

javainteger

提问by user446654

public class Three {
    public static void main(String[] args) {
        Three obj = new Three();
        obj.function(600851475143);
    }

    private Long function(long  i) {
        Stack<Long> stack = new Stack<Long>();

        for (long j = 2; j <= i; j++) {
            if (i % j == 0) {
                stack.push(j);
            }
        }
        return stack.pop();
    }
}

When the code above is run, it produces an error on the line obj.function(600851475143);. Why?

当上面的代码运行时,它会在行上产生一个错误obj.function(600851475143);。为什么?

回答by Thilo

You need to use a long literal:

您需要使用长文字:

obj.function(600851475143l);  // note the "l" at the end

But I would expect that function to run out of memory (or time) ...

但我希望该函数耗尽内存(或时间)......

回答by Yuliy

600851475143cannot be represented as a 32-bit integer (type int). It can be represented as a 64-bit integer (type long). long literals in Java end with an "L": 600851475143L

600851475143不能表示为 32 位整数(类型int)。它可以表示为 64 位整数(类型long)。Java 中的长字面量以“L”结尾:600851475143L

回答by Roman

Append suffix L: 23423429L.

附加后缀L23423429L.

By default, java interpret all numeral literals as 32-bit integer values. If you want to explicitely specify that this is something bigger then 32-bit integer you should use suffix Lfor long values.

默认情况下,java 将所有数字文字解释为 32 位整数值。如果要明确指定这是大于 32 位整数的L值,则应为长值使用后缀。

回答by Andre Holzner

You need 40 bits to represent the integer literal 600851475143. In Java, the maximum integer value is 2^31-1 however (i.e. integers are 32 bit, see http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html).

您需要 40 位来表示整数文字 600851475143。但是在 Java 中,最大整数值为 2^31-1(即整数为 32 位,请参阅http://download.oracle.com/javase/1.4.2/docs /api/java/lang/Integer.html)。

This has nothing to do with function. Try using a long integer literal instead (as suggested in the other answers).

这与function. 尝试改用长整数文字(如其他答案中所建议)。

回答by josefx

The java compiler tries to interpret 600851475143 as a constant value of type int by default. This causes an error since 600851475143 can not be represented with an int.

默认情况下,java 编译器尝试将 600851475143 解释为 int 类型的常量值。这会导致错误,因为 600851475143 不能用 int 表示。

To tell the compiler that you want the number interpretet as a long you have to add either lor Lafter it. Your number should then look like this 600851475143L.

要告诉编译器您希望将数字解释为 long,您必须在其后添加l或添加L。您的号码应如下所示600851475143L

Since some Fonts make it hard to distinguish "1" and lower case "l" from each other you should always use the upper case "L".

由于某些字体很难区分“1”和小写“l”,因此您应该始终使用大写“L”。

回答by JVM

At compile time the number "600851475143" is represented in 32-bit integer, try long literal instead at the end of your number to get over from this problem.

在编译时,数字“600851475143”以 32 位整数表示,请尝试在数字末尾使用 long 文字来解决此问题。

回答by Anand Undavia

Apart from all the other answers, what you can do is :

除了所有其他答案,您可以做的是:

long l = Long.parseLong("600851475143");

for example :

例如 :

obj.function(Long.parseLong("600851475143"));

回答by Milen.Jeremic

Or, you can declare input number as long, and then let it do the code tango :D ...

或者,您可以将输入数字声明为长,然后让它执行代码探戈:D ...

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.println("Enter a number");
    long n = in.nextLong();

    for (long i = 2; i <= n; i++) {
        while (n % i == 0) {
            System.out.print(", " + i);
            n /= i;
        }
    }
}