HomeSearch

Java Float Numbers

This Java page uses the float type. It uses float constants, casts to float values and passes floats as arguments to methods.
Float. This is a floating-point, 4-byte number. A float is like a double but only has 4 bytes (a double has twice as many bytes).
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.

Bytes: In Java we find the handy "BYTES" constants. These tell us exactly how many bytes are used by a type.

Java program that uses float 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); } } Output 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.
Java program that uses max, min floats 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); } } Output 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.
Java program that passes float, double arguments 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); } } Output 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.
Java program that causes float argument error public class Program { static void print(float value) { System.out.println(value); } public static void main(String[] args) { // This will not compile. print(100d); } } Output 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.

Narrowing: For a narrowing cast, like from double to float, some data loss is possible (although it might not occur).

Java program that causes double, float type error 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); } } Output 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.Casts
Java program that uses float 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); } } Output 200.75
Some research. I found that a normal number is a float that is within a balanced range. This is a technical detail. If you understand all the details, I admire your ability level.

Quote: In computing, a normal number is a non-zero number in a floating-point representation which is within the balanced range supported by a given floating-point format: it is a floating point number that can be represented without leading zeros in its significand.

Normal number: Wikipedia
A 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls