Overview
float is a primitive data type that stores decimal (floating-point) numbers.
It uses 4 bytes (32 bits) of memory and keeps about 7 significant digits.
It's a good choice when you need decimals and want to save memory, and you don't need high precision.
Java treats every decimal literal like 234.5 as a double by default. Add f or F at the end to make it a float.
Step-by-Step Explanation
1
Declare and Initialise float Variables
The f suffix is required.
float a = 234.5f;
float b = 234.565f;
float is the data type, a and b are the variable names, and 234.5f and 234.565f are float literals.
Without the f, Java gives a compile error because it can't automatically convert a double into a smaller float.
2
Print the Values
Text and values joined with +.
System.out.println("a = " + a);
System.out.println("b = " + b);
The + operator joins the text and the variable's value. The f suffix isn't printed, so the output is a = 234.5 and b = 234.565.
3
Limited Precision
Only about 7 significant digits are kept.
float pi = 3.14159265358979f;
System.out.println("pi = " + pi);
The value has 15 digits, but float can't store that many. Java rounds it, so the output is 3.1415927.
If you need more precision, use double.
4
Range of float
Largest and smallest positive values.
System.out.println("Max float value = " + Float.MAX_VALUE);
System.out.println("Min float value = " + Float.MIN_VALUE);
Float.MAX_VALUE is the largest float, 3.4028235E38. Float.MIN_VALUE is the smallest positive float, 1.4E-45, not the most negative one.
E38 means "× 1038".
float at a Glance
| Property |
Value |
| Size |
4 bytes (32 bits) |
| Precision |
About 7 significant digits |
| Largest value |
3.4028235E38 |
| Smallest positive value |
1.4E-45 |
| Default value (fields) |
0.0f |
| Literal suffix |
f or F |
| Wrapper class |
Float |
float vs double
| Feature |
float |
double |
| Size |
4 bytes |
8 bytes |
| Precision |
About 7 digits |
About 15 to 16 digits |
| Suffix needed |
Yes (f) |
No (default for decimals) |
| Best for |
Large arrays, graphics, saving memory |
Most general calculations |
Output
a = 234.5
b = 234.565
pi = 3.1415927
Max float value = 3.4028235E38
Min float value = 1.4E-45
Common Mistakes
Missing the f suffix
float a = 234.5; gives the error incompatible types: possible lossy conversion from double to float.
Using float for money
float can't store most decimal values exactly, so small rounding errors appear. Use BigDecimal for currency.
Thinking MIN_VALUE is negative
Float.MIN_VALUE is the smallest positive value. The most negative float is -Float.MAX_VALUE.
Good practice
Use double by default for decimal numbers, and use float only when memory matters or an API requires it.
Key Takeaway
float stores decimal numbers in 4 bytes with about 7 digits of precision.
Always add the f suffix to float literals, and use double when you need more precision
No previous program
No next program
📚 Learning Subject
Master Programming Through Practical Examples
Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.
🎯 How to learn from this example
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
🔥 Practice suggestion
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.