This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 38033
    
  22
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: 14074
    
  16

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 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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: 38033
    
  22
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: literal suffixes for certain primitive types
 
Similar Threads
primitive types in java
casting and converting
Storing Primitive Values in Vectors
Is it possible to construct the same object twice?
Please brief on JavaScript architecture