Convert int to float in Java

In this Java core tutorial, we learn how to convert int value into float value in Java via different solutions.

Assign int variable to float variable

ConvertIntToFloatExample1.java

public class ConvertIntToFloatExample1 {
    public static void main(String[] args) {
        int value1 = 87654;
        float value2 = value1;

        System.out.println("int value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
int value: 87654
float value: 87654.0

Using Integer.floatValue() method to get float value from an Integer object

ConvertIntToFloatExample2.java

public class ConvertIntToFloatExample2 {
    public static void main(String[] args) {
        Integer value1 = 123456;
        float value2 = value1.floatValue();

        System.out.println("int value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
int value: 123456
float value: 123456.0

Happy Coding 😊