Way2Java

long to float Java

After knowing primitive data types and Java rules of Data Type Casting (Type Conversion), let us cast long to float as an example.

A float carries a mantissa part (value of decimal point) where as long takes a whole number. Assignment of long to float is done implicitly. Observe the following order of data types.

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 float to int requires explicit casting.

Examples of implicit casting

int i1 = 10; // 4 bytes
long l1 = i1; // 4 bytes to 8 bytes

byte x = 10; // 1 byte
short y = x; // 1 byte to 2 bytes

byte x = 10; // 1 byte
int y = x; // 1 byte to 4 bytes

Following program on long to float explains implicit casting, Java style a long value is assigned to a float. It is equivalent to a simple assignment.
public class Conversions
{
  public static void main(String args[])
  {
    long l1 = 10;           
    float f1 = l1;      

    System.out.println("long value: " + l1);                  // prints 10
    System.out.println("Converted float value: " + f1);     // prints 10.0
  }
}



Output screenshot of long to float Java

float f1 = l1;

As long value l1 is assigned to float f1, the whole number is converted into floating-point value 10.0.

View all for 65 types of Conversions