0

I am writing a code in which I am trying to assign a value in long variable. But java compiler is showing error that too large integer number. I am trying to store 600851475143 in long type still.

class Sum {
    static public void main(String args[]){
        long num=600851475143;
    }
}
  • 2
    Note that it is standard practice to put `public` before `static`: `public static void main(...)` – assylias Feb 16 '13 at 19:06
  • When asking questions, remember to also post the *real [compiler] error message* and, better, *search for the error* before asking a question .. –  Feb 16 '13 at 19:11
  • http://stackoverflow.com/questions/3757763/integer-number-too-large-error-message-for-600851475143 , http://stackoverflow.com/questions/8924896/java-long-number-too-large-error - from `[java] integer too large` search .. –  Feb 16 '13 at 19:13
  • I wll take care of it... Thank you,assylias – user1988039 Feb 20 '13 at 13:44

2 Answers2

9

append 'L' or 'l' at the end of the number to make it a long literal.you can use both lowercase(l)or uppercase(L), but uppercase(L) is recommend for readability.

 long num=600851475143L;
PermGenError
  • 45,977
  • 8
  • 87
  • 106
3

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

Reference

So use this -

long num=600851475143l;

or better

long num=600851475143L;
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103