• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

literal suffixes for certain primitive types

 
Leroy J Brown
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
 
Keith Nagle
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic