int to float Java

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

The int and float, each takes 4 bytes of memory. The int holds a whole number and float holds a floating-point number. Assignment of int to float is done automatically by JVM.

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;
long l1 = i1;

byte x = 10;
short y = x;

Following program on int to float explains implicit casting. Observe, Java style where int is automatically assigned to float.
public class Conversions
  public static void main(String args[])
    int i1 = 10;                 // 4 bytes
    float f1 = i1;               // int is type converted to float

    System.out.println("int value: " + i1);                  // prints 10
    System.out.println("Converted float value: " + f1);      // prints 10.0

int to float
Output screenshot of int to float Java

float f1 = i1;

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

View all for 65 types of Conversions

Leave a Comment

Your email address will not be published.