float to double Java

float to double Java

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.

byte –> short –> int –> long –> float –> double

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.

float to double
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.

View all for 65 types of Conversions

Leave a Reply

Your email address will not be published. Required fields are marked *