11

I have to use quite a big number - 600851475143; as we all know, I have to use long data type but when I try to initialize like: long number = 600851475143 I get an error:

The literal 600851475143 of type int is out of range.

It seems that I don't know how to use long data type correctly.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Paulius
  • 141
  • 1
  • 1
  • 6

3 Answers3

24
long number = 600851475143L

Use "L" to make it as long type

RaceBase
  • 18,428
  • 47
  • 141
  • 202
  • 1
    Hi, could you add further what the L is and how we can use it in future number declaration? Thanks – Dean Meehan Nov 11 '13 at 21:05
  • 2
    @DeanMeehan, L or l represents "long" type. So whenever you want to declare a number explicitly to long type, you must add "l" to the number. if you are assigning the data directly, you need to add "l" to make it long or if the data is assigned during runtime, the JVM will automatically convert the data type, which you don't need to worry – RaceBase Nov 12 '13 at 04:40
  • Thanks :) ..and is this the same for byte etc? – Dean Meehan Nov 19 '13 at 03:28
  • @DeanMeehan, nope. You can check Java documents for that :) – RaceBase Nov 19 '13 at 06:15
  • @Reddy Shouldn't you be using `l` instead of `L`? I guess it's a personal preference but I have met many programmers who think `L` would create a `Long`. – Chetan Kinger Jun 26 '15 at 06:42
6

Use "L" to make it as long type. By default all integer type variable(byte,int,long) is "int"

long num=600851475143L;

or

long num=600851475143l; // small 'L'
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Naren
  • 61
  • 3
2

Use

 long number = 600851475143L;
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115