Results 1 to 20 of 20
Like Tree3Likes
  • 2 Post By JosAH
  • 1 Post By JosAH

Thread: Using L to denote a long literal

  1. #1
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Using L to denote a long literal

    Hi,
    So a long is 64 bits and is denoted by suffixing an l or L to a number, e.g.

    long l = 4L;

    Apparently if you don't do this you end up with an int:

    long l = 4; // compiles and executes fine; are there any ramifications to this? At this point, l is a long, right, even though I assigned an int?

    It seems to matter if you try to assign a value > the max value for an int, e.g.

    long l = 4000000000 // 4bn; max value for int is approx 2.147bn; does not compile

    This is remedied by appending an L to the number:

    long l = 4000000000L // compiles

    Is this the only reason why you would need to append l or L a long? Is there something I missed?

    Thanks,
    m

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,739
    Rep Power
    7

    Default Re: Using L to denote a long literal

    If the assigned long is out of range of an int then you need to append an L.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: Using L to denote a long literal

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Using L to denote a long literal

    I guess I didn't miss anything then.
    Ok cheers.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Using L to denote a long literal

    And the trailing L can be used to make numbers look funny:

    Java Code:
    long Java= 0xDEAL;
    boolean ok= Java != 0xC0B0L;
    kind regards,

    Jos ;-)
    DarrylBurke and gimbal2 like this.
    The only person who got everything done by Friday was Robinson Crusoe.

  6. #6
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Using L to denote a long literal

    Very good Jos

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Using L to denote a long literal

    Quote Originally Posted by mikomi View Post
    Very good Jos
    Java Code:
    int I = 0x50C00L;
    kind regards,

    Jos ;-)
    The only person who got everything done by Friday was Robinson Crusoe.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Using L to denote a long literal

    Quote Originally Posted by JosAH View Post
    And the trailing L can be used to make numbers look funny:

    Java Code:
    long Java= 0xDEAL;
    boolean ok= Java != 0xC0B0L;
    kind regards,

    Jos ;-)
    My god, I didn't realize it was possible to append the L to a hexadecimal number. Until now I've had to limit myself to the known ones such as 0xDEADBEEF and such. The possibilities that are now open to me!

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Using L to denote a long literal

    You can also stick in URLs before any type of loop (for clarity ;-)

    Java Code:
    http://www.google.com
    for (long Java= 0xDEAL; Java != 0xC0B0L; Java--);
    kindest regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Using L to denote a long literal

    yeah yeah, a label + a single line comment :) Now you're just abusing the language. 20 hail Gossling's should cover your sin.

  11. #11
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Using L to denote a long literal

    So what happens there? It compiles but makes my computer sound like a hoover; IOW, it compiles, but hangs when executed.

  12. #12
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Using L to denote a long literal

    Ah, maybe it's the silly loop exit condition, nothing to do with the label?

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Using L to denote a long literal

    Quote Originally Posted by mikomi View Post
    Ah, maybe it's the silly loop exit condition, nothing to do with the label?
    Busy loops are CPU killers, yes.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: Using L to denote a long literal

    Quote Originally Posted by gimbal2 View Post
    yeah yeah, a label + a single line comment :) Now you're just abusing the language. 20 hail Gossling's should cover your sin.
    And 3 Our Javas

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Using L to denote a long literal

    Quote Originally Posted by gimbal2 View Post
    yeah yeah, a label + a single line comment :) Now you're just abusing the language. 20 hail Gossling's should cover your sin.
    Abusing the language? I beg your pardon? Java is plastered with syntactic checks all over the place and everything is defined; Java can hardly be abused, it's a sissie language. If embedding unicode characters in comment or adding funny labels or adding an L here and there, is the best we can do (abuse-wise), I'd say booboo to Gossling. ;-)

    kindest regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  16. #16
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Using L to denote a long literal

    I didn't know you would get angry! Sheesh!

    Alright I'm only 100 posts in and I'm already causing derailments, I am going to go ------> over there for a while.

  17. #17
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    7

    Default Re: Using L to denote a long literal

    Unicode can be embedded in more than comments. This is valid Java source:

    Java Code:
    \u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0048\u0065\u006c\u006c\u006f\u0020\u007b\u000a\u0009\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0061\u0072\u0067\u0073\u0029\u0020\u007b\u000a\u0009\u0009\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0022\u0048\u0065\u006c\u006c\u006f\u002c\u0020\u004a\u0061\u0076\u0061\u0021\u0022\u0029\u003b\u000a\u0009\u007d\u000a\u007d\u000a
    Get in the habit of using standard Java naming conventions!

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Using L to denote a long literal

    Quote Originally Posted by kjkrum View Post
    Unicode can be embedded in more than comments. This is valid Java source:

    Java Code:
    \u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0063\u006c\u0061\u0073\u0073\u0020\u0048\u0065\u006c\u006c\u006f\u0020\u007b\u000a\u0009\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0073\u0074\u0061\u0074\u0069\u0063\u0020\u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028\u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0061\u0072\u0067\u0073\u0029\u0020\u007b\u000a\u0009\u0009\u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074\u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0022\u0048\u0065\u006c\u006c\u006f\u002c\u0020\u004a\u0061\u0076\u0061\u0021\u0022\u0029\u003b\u000a\u0009\u007d\u000a\u007d\u000a
    Yes, but it is more fun in comment lines, especially with the \u000a character.

    kind regards,

    Jos
    DarrylBurke likes this.
    The only person who got everything done by Friday was Robinson Crusoe.

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,048
    Blog Entries
    7
    Rep Power
    23

    Default Re: Using L to denote a long literal

    Quote Originally Posted by gimbal2 View Post
    I didn't know you would get angry! Sheesh!

    Alright I'm only 100 posts in and I'm already causing derailments, I am going to go ------> over there for a while.
    Angry? I'm furious! Blood behind my eyeballs! And I haven't even started yet about those silly booleans! I want my one and zero back for true and false! And I want null to be false! Raaaarrrrggghhh!

    (harf, harf) kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  20. #20
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,063
    Rep Power
    8

    Default Re: Using L to denote a long literal

    Not only null, also numbers should be booleans so that this works again:

    Java Code:
    int b = -1;
    if(b = 10){
    
    }
    I can't port broken C code to broken Java code because this isn't possible.

Similar Threads

  1. Inserting literal code tags
    By SurfMan in forum Suggestions & Feedback
    Replies: 5
    Last Post: 02-26-2013, 03:47 PM
  2. The literal of type int is out of range?
    By Zigster in forum New To Java
    Replies: 3
    Last Post: 04-24-2012, 04:33 AM
  3. Change Literal String to File Path with speratorChar
    By Jeremiah in forum New To Java
    Replies: 2
    Last Post: 08-18-2011, 07:14 PM
  4. Replies: 2
    Last Post: 02-04-2011, 11:10 PM
  5. Regex - matching literal characters
    By racha0601 in forum Advanced Java
    Replies: 3
    Last Post: 04-07-2009, 11:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •