Previous
Next

java lang Float Class - java.lang.Float Class in Java

java.lang.Float Class

Float Class in Java

Class Float

The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.

This Float class provides several methods for converting a float to a String and a String to a float, as well as other constants and methods useful when dealing with a 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.

What is Java Float Class ?

The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.

In addition, this class provides several methods for converting a float to a String and a String to a float, as well as other constants and methods useful when dealing with a float.

Share This Page On :

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));
  }
}

How to convert String to Float in Java ?

There are three ways to convert a String to float in Java

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.

Convert String to Float in Java Example

/*  Convert String to Float in Java Example
    Save with file name ConvertStringToFloat.java  */
public class ConvertStringToFloat
{
  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"));
   
    //CONVERT STRING TO FLOAT USING CONSTRUCTOR
    Float float_value = new Float("333.55");
    System.out.println("String to Float using Constructor : "+float_value);
  }
}

More Related Tutorials

Previous
Next