Letter 'L'/'l' suffixed to a number for 'long' type

S

Spendius

Hi,
I found in some code the following 'sleep(1000L)': I'd just
like to know what padding an 'L' after 1000 does. What
would be the difference with a simple 'sleep(1000)' ?

Thanks.
Spendius
 
E

Eric Sosman

Roedy said:
L means it is a long literal. 1000 is an int. It will automatically
be promoted to long.

Clarification: Roedy does not mean that all int values
are automatically promoted to long, and indeed they are not.
But when the Java compiler sees `sleep(1000)' it first tries
to find a method named sleep that takes an int argument. If
no such method exists Java will look for alternatives. In
this case (assuming sleep is Thread.sleep), it finds that
there *is* a method named sleep that takes a long argument.
Since Java knows how to promote an int to a long, it makes
the conversion and calls the long-accepting sleep method.

By using `sleep(1000L)' in the first place, the writer
saved the compiler from all this rummaging around (not very
important) and also made it plain to people reading the code
that the call was to a method taking a long argument -- that
is, the writer saves *you* from the same rummaging around
that the compiler goes through.

To experiment with how this works, ponder the following
class and predict its output, then compile it and check
your predictions:

class Promote {
void m(double d) { System.out.println("double: " + d); }
void m(long l) { System.out.println("long: " + l); }
// void m(int i) { System.out.println("int: " + i); }
void m(short s) { System.out.println("short: " + s); }
void m(Object o) { System.out.println("object: " + o);

public static void main(String[] unused) {
m( (byte)1 );
m( (short)2 );
m( 3 );
m( 4L );
m( 5.0f );
m( 6.0 );
m( new Integer(7) );
m( "eight" );
}
}

For extra credit, predict what would happen if you removed
the // from the fourth line (and check your prediction).
 
E

Eric Sosman

Eric said:
[...]
To experiment with how this works, ponder the following
class and predict its output, then compile it and check
your predictions:
[...]

Well, I made a fine mess out of that one, didn't I? Try
this code instead, and accept my apologies for any confusion:

class Promote {
static void m(double d) {
System.out.println("double: " + d); }
static void m(long l) { System.out.println("long: " + l); }
// static void m(int i) { System.out.println("int: " + i); }
static void m(short s) {
System.out.println("short: " + s); }
static void m(Object o) {
System.out.println("object: " + o); }

public static void main(String[] unused) {
m( (byte)1 );
m( (short)2 );
m( 3 );
m( 4L );
m( 5.0f );
m( 6.0 );
m( new Integer(7) );
m( "eight" );
}
}
 
W

Wojtek

Spendius wrote :
Hi,
I found in some code the following 'sleep(1000L)': I'd just
like to know what padding an 'L' after 1000 does. What
would be the difference with a simple 'sleep(1000)' ?

Thanks.
Spendius

In addition to the other explanations, it also helps when for some
reason you need to refactor that method.

foo(100L);

where

public void foo(long value)
{
....
}

If in the future foo is changed to only accept an integer:

public void foo(int value)
{
....
}

then the compiler will complain about the change and you can make some
decision about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top