java.lang.Float Class

The Float class wraps the value of primitive data type float into Float object. An object of type Float contains a single field whose type is float. In other words the wrapper classes create objects for primitive data types. The wrapper classes are Boolean, Byte, Character, Short, Integer, Float, Long and Double.

This class has following constructors.

Float Class Constructors

Float(double value)

Constructs a newly allocated Float object that represents the argument converted to type float.

Float(float value)

Constructs a newly allocated Float object that represents the primitive float argument.

Float(String s)

Constructs a newly allocated Float object that represents the floating-point value of type float represented by the string.

java.lang.Float Class Example
/* Save with file name FloatExample.java */

public class FloatExample
{
	public static void main(String args[])
	{
		//FLOAT DECLARATION
		Float b;
		//MEMORY ALLOCATION FOR FLOAT
		b = new Float(101);
		//FLOAT CLASS OUTPUT
		System.out.println();
		System.out.println("Float Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURNS byte PRIMITIVE DATA TYPE
		System.out.println("byte Value is : "+ b.byteValue());
		//RETURNS short PRIMITIVE DATA TYPE
		System.out.println("short Value is : "+ b.shortValue());
		//RETURNS double PRIMITIVE DATA TYPE
		System.out.println("double Value is : "+ b.doubleValue());
		//RETURNS float PRIMITIVE DATA TYPE
		System.out.println("float Value is : "+ b.floatValue());
		//RETURNS int PRIMITIVE DATA TYPE
		System.out.println("int Value is : "+ b.intValue());
		//RETURNS long PRIMITIVE DATA TYPE
		System.out.println("long Value is : "+ b.longValue());
	}
}
                        
java.lang.Float Class Example 2
/* Save with file name FloatExample2.java */

public class FloatExample2
{
	public static void main(String args[])
	{
		//FLOAT DECLARATION
		Float b, b2;
		//MEMORY ALLOCATION FOR FLOAT
		b = new Float(101);
		b2 = new Float("102");
		//FLOAT CLASS OUTPUT
		System.out.println();
		System.out.println("Float Class Example");
		System.out.println("=====================");
		System.out.println();
		System.out.println("b Value is : "+ b.floatValue());
		System.out.println("b2 Value is : "+ b2.floatValue());
		System.out.println();
		System.out.println("b compareTo b2 : "+ b.compareTo(b2));
		System.out.println("b equals b2 : "+ b.equals(b2));
	}
}
                        

In the following example you can learn how to use static methods of Float Class. If you use static methods you need not to create the class instance. You can call static methods using the class name as reference.

java.lang.Float Class Example 3
/* Save with file name FloatExample3.java */

public class FloatExample3
{
	public static void main(String args[])
	{
		//FLOAT CLASS STATIC METHODS USAGE
		System.out.println("Float Class Example");
		System.out.println("=====================");
		System.out.println();
		//RETURN Float OBJECT WITH THE SPECIFIED PRIMITIVE float DATA TYPE
		System.out.println("valueOf : "+ Float.valueOf(101));
		//RETURN Float OBJECT WITH THE SPECIFIED
		//PRIMITIVE float DATA TYPE AS String
		System.out.println("valueOf : "+ Float.valueOf("103"));
		//RETURN PRIMITIVE float DATA TYPE WITH THE SPECIFIED
		//PRIMITIVE float DATA TYPE AS String
		System.out.println("parseFloat : "+ Float.parseFloat("105"));
	}
}