Floating Point Types

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

Is the following an integer literal? 197.0

Answer:

No — it has a decimal point.

Floating Point Types

If you use the literal 197.0 in a program, the decimal point tells the compiler to represent the value using a floating point primitive data type. The bit pattern used for floating point 197.0 is very much different than that used for the integer 197. There are two floating point primitive types.

Floating Point Primitive Data Types
TypeSizeRange
float32 bits-3.4E+38 to +3.4E+38
double64 bits-1.7E+308 to 1.7E+308

Data type float is sometimes called "single-precision floating point". Data type double has twice as many bits and is sometimes called "double-precision floating point". These phrases come from the language FORTRAN, at one time the dominant programming language.

In programs, floating point literals have a decimal point in them, and no commas (no thousand's separators):

123.0        -123.5         -198234.234       0.00000381

Note: Literals written like the above will automatically be of type double. Almost always, if you are dealing with floating point numbers you should use variables of type double. Then the data type of literals like the above will match the data type of your variables. Data type float should be used only for special circumstances (such as when you need to process a file of data containing 32 bit floats).

QUESTION 8:

(Thought question: ) Do you think that using float instead of double saves a significant amount of computer memory?