Here we wii see how to create a float number from primitive types in Java.
Source Code
package com.beginner.examples;
public class TestFloat {
public static void main(String[] args) {
//Create a Float object from float primitive type
float f = 3.14f;
System.out.println("1. "+f);
//Create a Float object from double primitive type
double d = 3.14;
Float f1 = new Float(d);
System.out.println("2. "+f1);
//Create a Float number from the string
Float f2 = new Float("3.14");
System.out.println("3. "+f2);
}
}
Output:
1. 3.14
2. 3.14
3. 3.14