QR Code for this Page
java lang Float Class

java lang Float Class

java.lang.Float Class

Float Class in Java

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.

Java Float class provides several useful methods for converting a float to a String and a String to a float, as well as other constants and methods.

java.lang.Float Class Example

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

public class FloatExample
{
	public static void main(String args[])
	{
		//JAVA FLOAT DECLARATION

		Float b;

		//MEMORY ALLOCATION FOR JAVA FLOAT

		b = new Float(101);

		//FLOAT CLASS OUTPUT

		System.out.println("Java Float Class Example");

		//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

/* Java Float Class Example 2 */
/* Save with file name FloatExample2.java */

public class FloatExample2
{
	public static void main(String args[])
	{
		//JAVA FLOAT DECLARATION

		Float b, b2;

		//MEMORY ALLOCATION FOR JAVA FLOAT

		b = new Float(101);

		b2 = new Float("102");

		//JAVA FLOAT CLASS OUTPUT

		System.out.println("Java Float Class Example");

		System.out.println("b Value is : "+ b.floatValue());

		System.out.println("b2 Value is : "+ b2.floatValue());

		System.out.println("b compareTo b2 : "+ b.compareTo(b2));

		System.out.println("b equals b2 : "+ b.equals(b2));
	}
}

Following Java Float class example you can learn how to use static methods of Java 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

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

public class FloatExample3
{
	public static void main(String args[])
	{
		//JAVA FLOAT CLASS STATIC METHODS USAGE

		System.out.println("Java Float Class Example");

		//RETURN Float OBJECT WITH THE SPECIFIED PRIMITIVE float DATA TYPE

		System.out.println("valueOf : "+ Float.valueOf(101));

		//RETURN Java 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"));
	}
}