Home
Java
float Number
Updated May 31, 2025
Dot Net Perls
Float. This is a floating-point, 4-byte number. In Java a float is like a double but only has 4 bytes (a double has twice as many bytes).
double
With this type, we can store numbers with a decimal place. A float can be cast to a double without any special syntax. But we must be careful when converting from a double to a float.
Float examples. Here are two syntax forms for creating a float value. We can use (float) to specify the type. And the lowercase "f" specifies a float literal.
Tip In Java we find the handy "BYTES" constants. These tell us exactly how many bytes are used by a type.
public class Program {
    public static void main(String[] args) {

        // Use a float variable.
        // ... Uses a float cast.
        float number = (float) 10.5;
        System.out.println(number);

        // This syntax also works.
        float number2 = 10.75f;
        System.out.println(number2);

        // A float is 4 bytes.
        System.out.println(Float.BYTES);
    }
}
10.5 10.75 4
Max, min values. Floats can store a wide range of values. But with floats we also find a "normal values" concept. A normal value is within a "balanced range" of values.
public class Program {
    public static void main(String[] args) {
        float max = Float.MAX_VALUE;
        float minNormal = Float.MIN_NORMAL;
        float min = Float.MIN_VALUE;

        // Print some float values.
        System.out.println(max);
        System.out.println(minNormal);
        System.out.println(min);
    }
}
3.4028235E38 1.17549435E-38 1.4E-45
Float, double arguments. Suppose we have a method that receives a double argument. We can pass floats or doubles to it—these values are all "widened" to match the method signature.
public class Program {

    static void print(double value) {
        System.out.println(value);
    }

    public static void main(String[] args) {

        // We can pass a float or a double as a double argument.
        print(100f);
        print(100d);
    }
}
100.0 100.0
Float argument error. We cannot pass a double to a method that requires a float. An error will occur. For this reason, requiring doubles as the parameter type in methods is more compatible.
public class Program {

    static void print(float value) {
        System.out.println(value);
    }

    public static void main(String[] args) {

        // This will not compile.
        print(100d);
    }
}
The method print(float) in the type Program is not applicable for the arguments (double)
Cannot convert from double. We cannot convert a double to a float without an explicit cast expression. This prevents data loss when you might not expect it.
Detail For a narrowing cast, like from double to float, some data loss is possible (although it might not occur).
public class Program {
    public static void main(String[] args) {

        // This will not compile.
        // ... We must cast the double to a float.
        double valueD = 100.5d;
        float valueF = valueD;
        System.out.println(valueF);
    }
}
Type mismatch: cannot convert from double to float
Float cast. With an explicit cast, we can convert a double to a float. We can also specify casts when not required just to make our code clearer.
Cast
public class Program {
    public static void main(String[] args) {

        // Cast double to float.
        double valueD = 200.75d;
        float valueF = (float) valueD;
        System.out.println(valueF);
    }
}
200.75
Summary. Floats are a 4-byte floating point format. We must cast doubles to floats with an expression. But for going from float to double, in a widening cast, no expression is needed.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on May 31, 2025 (edit).
Home
Changes
© 2007-2025 Sam Allen