aspose file tools*
The moose likes Beginning Java and the fly likes literal suffixes for certain primitive types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "literal suffixes for certain primitive types" Watch "literal suffixes for certain primitive types" New topic
Author

literal suffixes for certain primitive types

Leroy J Brown
Ranch Hand

Joined: Dec 02, 2007
Posts: 71
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

Joined: Oct 13, 2005
Posts: 39851
    
  28
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

Joined: Aug 16, 2005
Posts: 14348
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
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 ]

SCJP 5.0
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39851
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: literal suffixes for certain primitive types