0

Statement like this compiles as well. Please let me know, what can be the intention behind using such statements ?

Akash Nigam
  • 300
  • 2
  • 5
  • 11

5 Answers5

2

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.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
  • 1
    You'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
  • 1
    It'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
  • That's what I wrote :) – Crazyjavahacking Apr 03 '15 at 18:51
  • 1
    "does not fin into long type" doesn't quiet say the same thing. – candied_orange Apr 03 '15 at 18:52
  • 1
    Constants 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
1

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 ints and the L is not used, the expression could overflow before it reaches x.

teppic
  • 8,039
  • 2
  • 24
  • 37
0
int a = 12L; 

Initialize a long and assign it to a

Michele d'Amico
  • 22,111
  • 8
  • 69
  • 76
Razib
  • 10,965
  • 11
  • 53
  • 80
0

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;

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

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;.