Statement like this compiles as well. Please let me know, what can be the intention behind using such statements ?
-
the intention? Probably just the programmer showing off his knowledge of narrowing primitive conversions =) – Apr 03 '15 at 18:33
-
2Why is this tagged both `java` and `c`? – R.. GitHub STOP HELPING ICE Apr 03 '15 at 18:35
-
sorry, thought it might work for Java as well.. Removed 'java' tag – Akash Nigam Apr 03 '15 at 18:39
5 Answers
The L
postfix is only useful when you need to define a long constant which you cannot really define otherwise, e.g.:
long l = 10000000000; // will not compile as the number does not fin into long type
long l = 10000000000L; // compiles and you have to explicitly define the L
In your case it does not matter whether you use L
or not.

- 9,343
- 2
- 31
- 40
-
1You're the only one who actually answers the question (the "what for"? part), +1. – michaelmeyer Apr 03 '15 at 18:39
-
The first one compiles fine, it's automatically promoted to a long since if necessary. – teppic Apr 03 '15 at 18:42
-
1It's not about the number not fitting into the long type. It's about not fitting into the nameless temporary int that you are about to assign. Without an L at the end every number tries to be an int before you make it into something else. This wont work if the number you type is longer than an int will hold. If the number is 12 it fits in an int so no one cares. – candied_orange Apr 03 '15 at 18:49
-
-
1"does not fin into long type" doesn't quiet say the same thing. – candied_orange Apr 03 '15 at 18:52
-
1Constants are promoted to the type that can hold them automatically, "The type of an integer constant is the first of the corresponding list in which its value can be represented." 6.4.4.1.5 That list includes `long long`. It tries `int` first, then `long`, so these two statements are essentially identical. – teppic Apr 03 '15 at 18:58
-
@teppic Yes but that wasn't always true. I think it's best explained here: http://stackoverflow.com/a/18707097/1493294 – candied_orange Apr 03 '15 at 19:04
-
@CandiedOrange - we should only quote C89 though when necessary since it's an obsolete standard. C99 contradicts the answers here. – teppic Apr 03 '15 at 19:06
-
@teppic it is necessary here since the main reason you'll see an L suffix is historical. Old code never dies. It just rots. – candied_orange Apr 03 '15 at 19:09
-
@CandiedOrange looking at that other question, this code would be fine in C89 too. It'd either be an unsigned int or a long. – teppic Apr 03 '15 at 19:12
With a line like this, there's no point at all. 12L
is a long integer, and so the constant has a long type, but it can always be represented by an int
. C guarantees that an int must be equal or less than the length of a long.
By default the constant 12
is an int
-- any constant that requires a long
will automatically be long. In this case the constant is automatically narrowed to an int
in the assignment. Remember that the assignment operator is not a bit-for-bit copy, it often involves implicit type conversions.
The usual case for a suffix like this is to force arithmetic with a wider type than is necessary in each part of the expression, e.g.
long x = (1000L * a * b);
If a
and b
are int
s and the L is not used, the expression could overflow before it reaches x
.

- 8,039
- 2
- 24
- 37
int a = 12L;
Initialize a long and assign it to a

- 22,111
- 8
- 69
- 76

- 10,965
- 11
- 53
- 80
-
but long occupies 8 bytes of memory. How can an integer variable (that occupies only 4 bytes of memory) hold a value that is 8 bytes long ?? – Akash Nigam Apr 03 '15 at 18:31
-
@AkashNigam it doesn't matter if you use 512bits to represent 12, the number still fits in 1 byte. – teppic Apr 03 '15 at 18:37
-
@teppic : true, but the memory occupied by 12L will still be 64 bits right ? – Akash Nigam Apr 03 '15 at 18:42
-
@AkashNigam the constant 12L occupies as many bits as a long, but it can be held in 8 bits without any loss. – teppic Apr 03 '15 at 18:45
-
@ teppic : so this is something similar to : long x = 12; int a = (int)x; correct ? – Akash Nigam Apr 03 '15 at 18:46
-
@AkashNigam yes. But since 12L by itself never has to be long, it's pointless. – teppic Apr 03 '15 at 18:48
int a = 12L;
returns :
java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from long to int
long a = 10000000000;
returns :
java.lang.Error: Unresolved compilation problem:
The literal 10000000000 of type int is out of range
So you can use it as long a = 10000000000L;

- 6,357
- 5
- 40
- 55
It will be implicitly casted:
int a = 12L; is equivalent to int a = (int)12L;
Btw: In my IDE (Eclipse Kepler) it won't compile with int a = 12L;
.