Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Integer number too large" error message for 600851475143

Tags:

java

integer

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?

like image 512
user446654 Avatar asked Sep 21 '10 06:09

user446654


People also ask

How do you fix an integer too large?

If you are dealing with numbers in your Java program and it gets too long! (over 2,147,483,647 to be precise) then it a type long and not int so you need to suffix the letter capital L or lower case l to fix the compilation error.

What is long in Java?

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1.


1 Answers

600851475143 cannot 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

like image 80
Yuliy Avatar answered Sep 19 '22 19:09

Yuliy