How To Convert Int To Float In Java

Converting an int to a float in Java can be done by explicitly casting the int to a float or by using the Float.valueOf method. In Java, the conversion from int to float takes place automatically when you assign an int value to a float variable. Widening casting or widening primitive conversion are other names for this. As a result, you may simply assign a value of int to float to widen cast an in into a float.

Approaches for converting an int to a float in Java

In Java, there are two main approaches for converting an int to a float:

  1. Explicit casting: The int can be explicitly cast to a float by placing the int in parentheses followed by the float type in parentheses.
  2. Using Float.valueOf method: The Float.valueOf method can be used to convert an int to a float.

Have a look at example of both approaches for converting an int to a float in Java

Approach 1: Explicit casting

Here’s an example of converting an int to a float in Java using explicit casting:

public class Main {
    public static void main(String[] args) {
        // Define an integer variable
        int integer = 10;
         // Convert the integer to a float by explicit casting
        float floatNumber = (float) integer;
        
        // Print the converted float value
        System.out.println("Converted float: " + floatNumber);
    }
}

Output:

Converted float: 10.0

Explanation of code:

  • The main method declares an integer variable named “integer” with a value of 10.
  • The integer value is then explicitly cast to a float and stored in a float variable named “floatNumber”.
  • The value of the “floatNumber” variable is printed to the console using the System.out.println method with the message “Converted float: ” followed by the value of the “floatNumber” variable.

Approach 2: Using Float.valueOf method

Here’s an example of using Float.valueOf() method to convert an int to a float in Java with comments, explanation, and output:

public class Main {
  public static void main(String[] args) {
    int num = 42;
    // Convert int to float using Float.valueOf() method
    Float floatNum = Float.valueOf(num);
    System.out.println("Converted float value: " + floatNum);
  }
}

Output:

Converted float value: 42.0

Explanation of code:

  • int value 42 is assigned to the variable num.
  • Float.valueOf(num) method is used to convert int to float.
  • The converted float value is then printed using println() method.

Best Approach for converting int to float

‘Float.valueOf()’ method is considered a good choice for converting an int to a float in Java for several reasons:

  1. Type Safety: It returns a Float object, which is the object representation of a float value. This helps in avoiding accidental type conversions and ensures type safety.
  2. Readability: Using Float.valueOf() makes the code more readable and self-explanatory, as it clearly indicates that a conversion from int to float is taking place.
  3. Caching: Float.valueOf() method caches commonly used float values, which can improve performance for frequently used values.
  4. Nullability: If the value passed to Float.valueOf() method is null, it will return null, which can be useful in handling null values in the code.

Sample Questions for converting int to float

Sample Question 1

Write the code for converting int to float using Explicit casting

public class Main {
  public static void main(String[] args) {
    int num = 35;
    // Convert int to float using explicit casting
    float floatNum = (float) num;
    System.out.println("Converted float value: " + floatNum);
  }
}

Output:

Converted float value: 35.0

Sample Question 2

Write the code for converting int to float using ‘Float.valueOf()’ method

public class Main {
  public static void main(String[] args) {
    int num = 42;
    // Convert int to float using Float.valueOf() method
    Float floatNum = Float.valueOf(num);
    System.out.println("Converted float value: " + floatNum);
  }
}

Output:

Converted float value: 42.0

Conclusion

Float is a larger data type, and int is a smaller one. As a result, Java automatically converts an int value to a float when you assign it to a float variable. Widening casting or widening primitive conversion are other names for this.

As a result, you may simply assign a value of int to float to widen cast an in into a float.