LAST UPDATED: AUGUST 25, 2020
Java Integer floatValue() Method
Java floatValue()
method belongs to the Integer
class of the java.lang
package. This method returns the floating equivalent of the Integer after a widening primitive conversion(Conversion of a lower data type into a higher data type).
In short, this method is used to convert an Integer object into a float value.
Syntax:
public float floatValue()
Parameter:
No parameter is passed in this method.
Returns:
The float equivalent of the Integer object that is created after conversion.
Example 1:
Here, using the floatValue()
function the Integer object is converted into its float equivalent.
import java.lang.Integer;
public class StudyTonight
{
public static void main(String[] args)
{
//converting integer object into float
Integer x = 65;
float i=x.floatValue();
System.out.println(i);
Integer y = 90;
float d = y.floatValue();
System.out.println(d);
}
}
65.0
90.0
Example 2:
Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent float value.
import java.util.Scanner;
public class StudyTonight
{
public static void main(String[] args)
{
try
{
System.out.print("Enter the value to be converted : ");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
Integer n = i ;
float val = n.floatValue(); //converting integer object into float
System.out.println("Float Value is: " + val);
}
catch(Exception e)
{
System.out.println("not a valid integer");
}
}
}
Enter the value to be converted: 23
Float Value is: 23.0
Live Example:
Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.