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

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();

    }

}

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


米脂
浏览 668回答 3
3回答

一只名叫tom的猫

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

慕码人8056858

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

凤凰求蛊

您需要使用长文字:obj.function(600851475143l);&nbsp; // note the "l" at the end但我希望该功能用完内存(或时间)...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java