Home› Java Programming Language› / Programs / Java Program to Demonstrate the float Data Type
🚀 Programming Example

Java Program to Demonstrate the float Data Type

2,655 Views
Java Java 8 or later
Beginner
Demonstration

Learn how to declare, initialise and print float variables in Java. This program explains why the f suffix is required and shows that float keeps only about 7 significant digits. It also prints the range of the type using Float.MAX_VALUE and Float.MIN_VALUE.

Before You Begin

What you will learn

  • Declare and initialise variables of the float data type
  • Understand why a float literal needs the f or F suffix
  • Know that float uses 4 bytes and keeps about 7 significant digits
  • Print the range of float using Float.MAX_VALUE and Float.MIN_VALUE
  • Join text and values in output using the + operator
  • Choose between float and double

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • Basic structure of a Java program: a class and the main() method
  • How to print output with System.out.println()
  • What a variable is and how to declare one
  • Single-line comments using //
No previous program
No next program

📌 Information & Algorithm

Algorithm

  1. Start
  2. Create a public class named FloatDataType.
  3. Define the main() method.
  4. Declare float variable a and assign it 234.5f.
  5. Declare float variable b and assign it 234.565f.
  6. Display the values of a and b.
  7. Declare float variable pi with a long decimal value and display it, showing that only about 7 digits are kept.
  8. Display Float.MAX_VALUE and Float.MIN_VALUE.
  9. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac FloatDataType.java
java FloatDataType

💻 Program Code

public class FloatDataType {

    public static void main(String[] args) {

        // Declare and initialise float variables
        // The 'f' suffix tells Java the literal is a float, not a double
        float a = 234.5f;
        float b = 234.565f;

        System.out.println("a = " + a);
        System.out.println("b = " + b);

        // float keeps only about 7 significant digits
        float pi = 3.14159265358979f;
        System.out.println("pi = " + pi);

        // Range of the float data type
        System.out.println("Max float value = " + Float.MAX_VALUE);
        System.out.println("Min float value = " + Float.MIN_VALUE);
    }
}

Sample Input

No input required

🖥 Program Output

                                a = 234.5
b = 234.565
pi = 3.1415927
Max float value = 3.4028235E38
Min float value = 1.4E-45
                            

📘 Explanation

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

Complexity

Time: O(1)

Space: O(1)

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.