After knowing Java rules of Data Type Casting (Type Conversion), let us cast float to double. By memory-wise, float takes 4 bytes of memory and double takes 8 bytes of memory. Assignment of 4 bytes value to 8 bytes value is done implicitly.
See the data types order to know the brief rules of casting.
The left-side value can be assigned to any right-side value and is done implicitly. The reverse like double to float requires explicit casting.
Examples of implicit casting
float x = 10.5f;
double y = x;
byte x = 10; // 1 byte
int y = x; // 1 byte to 4 bytes
Following program on float to double explains implicit casting. It looks simple assignment. Anyhow, observe Java syntax.
public class Conversions
public static void main(String args)
float f1 = 10.5f; // 4 bytes
double d1 = f1; // type converted 4 bytes float to 8 bytes double
System.out.println("float value: " + f1); // prints 10.5
System.out.println("Converted double value: " + d1); // prints 10.5
Output screenshot of float to double Java
float f1 = 10.5f;
We know earlier in data types, the float value must be suffixed with either F or f; else it is a compilation error.
double d1 = f1;
The float f1 value is automatically promoted to double d1 value. This is known as automatic promotion or widening conversion.