Let us say that you wanted to deal with the number 1,023,004 in your computer program. Would data type short be an appropriate choice?
No. Data of type short can be only in the range -32,768 to +32,767.
Larger ranges of numeric values require more bits. The different sizes for integer data enable you to pick an appropriate size for the data you are working with. Usually you should pick a data type that has a range much greater than the range of numbers you expect to deal with. If a program uses only a few dozen variables it will run just as fast and take up about as much main memory no matter what size is used for its variables.
Why do the small sized data types exist, then?
Well, many real-world programs deal with massive amounts of data
(billions of data items) and then
using the smaller sizes may save significant amounts of space and time.
But we will not use that much data in these notes.
Usually you should use int
or double
for your numeric data.
When you write a program you do not have to know how to represent a number in bits. You can type the number just as you would on a typewriter. This is called a literal. Integer literals in a program are written as in a book, except there are no commas:
125 -32 16 0 -123987
All of the above examples are 32 bit int
literals.
A 64 bit long
literal has a upper case 'L' or lower case 'l'
at the end.
However, NEVER use the lower case 'l' because it is easily confused
with a digit '1'.
125L -32L 16L 0l -123987l
The last two examples are legal, but confusing.