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.
Detail 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.
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
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.