Float wrapper class is used to wrap primitive data type float value in an object.
Example of Float Wrapper Class
Example 1 : Convert float to Float object
public class floatToFloat
{
public static void main(String[] args)
{
float f = 1.2f;
Float fObj = new Float(f);
System.out.println(fObj);
}
}
Output
1.2
This example shows how a float primitive value can be converted to a Float object.
Example 2 : Convert String to Float Object
public class StringToFloat
{
public static void main(String[] args)
{
Float fObj1 = new Float("1.2");
System.out.println(fObj1);
String str1 = "2.4";
Float fObj2 = Float.valueOf(str1);
System.out.println(fObj2);
String str2 = "4.6";
float f = Float.parseFloat(str2);
System.out.println(f);
}
}
Output
1.2
2.4
4.6
This example shows how we can convert String object to Float object.