• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

literal suffixes for certain primitive types

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One topic I've been meaning to ask about is the suffixes appended to literals when assigning to certain primitive types.



Can someone please explain (or point to a resource that explains) on what primitive types and under what circumstances these suffixes need to be applied and why this is necessary?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On long literals: because the number of digits you have written won't fit into an int. Because 1L is better than (long)1.

On float literals because 1f or 1.23f is better than (float)1.23.

On double literals: only worth doing if there is no decimal point, because 123d is better than (double)123.

One of the few places where Java is case-insensitive, but always use upper-case L, never lower-case l on longs.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An integer literal is by default a 32-bit integer, so it can hold values in the range -(2^31) to (2^31)-1. If you write an integer literal which is outside that range (such as 9000000000000000000), the compiler will give you an error, because it doesn't fit in an int. By appending "L" (or "l") you tell the compiler that it's a long literal (64-bit).

The same with floating-point numbers: by default, a floating-point literal is a double. Sometimes you want to specify float literals explicitly, so you add an "F" (or "f") to the number.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
One of the few places where Java is case-insensitive, but always use upper-case L, never lower-case l on longs.


I agree. Lower-case l can sometimes be confused for 1. I've made that mistake quite some times already
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I would just contribute something (small) that had me
perplexed when i first came across primitives.
Consider int - 32bits in size.
So it's range is (as stated above!):
-(2^31) all the way to (2^31) - 1.
Why do you subtract the 1 from the positive range?
Because... 0 is counted as a positive integer.
Maybe you wondered why you subtract 1, maybe you didn't.
Hope it helps anyhow.
Best regards.
[ June 20, 2008: Message edited by: Keith Nagle ]
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be more precise, Keith Nagle, to say negative and non-negative: exactly 50% each, but it is not a good idea to ask what is a different question in somebody else's thread.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic