Introducing Floating-Point Types: float and double
You'll learn what floating-point types are and when to use them instead of integers.
What Are Floating-Point Types?
So far, you've worked with integer types like int, long, short, and byte — perfect for whole numbers. But what about measurements like temperature (98.6°F), distances (3.14 miles), or prices ($19.99)? For values with decimal points, Java provides floating-point types: float and double.
Think of floating-point numbers like a scientific calculator that can handle both whole numbers and fractions, whereas integer types are like a basic counter that only clicks through whole numbers.
The Two Floating-Point Types
double (64-bit): The default choice for decimal numbers in Java. It stores about 15-17 significant digits with high precision. The name means "double precision."
float (32-bit): Uses half the memory of double but with lower precision (about 6-7 significant digits). You must add an f or F suffix to float literals, similar to how long uses L.
When to Use Each
Use double for most situations requiring decimal values — it's Java's default floating-point type, just like int is the default integer. Use float only when memory is extremely tight and you're working with large arrays of decimal numbers where slightly reduced precision is acceptable.
Key Takeaway: Floating-point types (float and double) represent numbers with decimal points; double is the 64-bit default with high precision, while float is a 32-bit alternative with lower precision that requires an f suffix on literals.