More Bits for More Range

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Can data type short hold the value 1,023,004 ?

Answer:

No. Data of type short can be only in the range -32,768 to +32,767.

More Bits for More Range

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. The word "literal" means that a value is explicitly part of the program. For example, 125 literally repesents the value one hundred twenty five. 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.

QUESTION 7:

Is the following an integer literal?

197.0