Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Spendius - 30 Jul 2007 10:28 GMT
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
Roedy Green - 30 Jul 2007 11:24 GMT
>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)' ?
L means it is a long literal. 1000 is an int.  It will automatically
be promoted to long.
see http://mindprod.com/jgloss/literal.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Eric Sosman - 30 Jul 2007 13:46 GMT
>> 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)' ?
> 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).

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Eric Sosman - 30 Jul 2007 14:20 GMT
> [...]
>     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" );
       }
    }

Signature

Eric Sosman
esosman@ieee-dot-org.invalid

Roedy Green - 30 Jul 2007 15:46 GMT
>     Well, I made a fine mess out of that one, didn't I?  Try
>this code instead, and accept my apologies for any confusion:
For yet another stab at explaining it, please see
http://mindprod.com/jgloss/overload.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Wojtek - 30 Jul 2007 16:51 GMT
Spendius wrote :
> Hi,
> I found in some code the following 'sleep(1000L)': I'd just
[quoted text clipped - 3 lines]
> 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.

Signature

Wojtek :-)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.