Convert string value to float value

static float parseFloat(String s)
Returns a new float initialized to the value represented by the specified String, as performed by the valueOf method of class Float.
static Float valueOf(float f)
Returns a Float instance representing the specified float value.
static Float valueOf(String s)
Returns a Float object holding the float value represented by the argument string s.

public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.parseFloat("1.1F");
    System.out.println(floatObject2);
    
  }
}

The output:

1.1

You cannot use floating-point literal as Java think it is a double.


public class Main {
  public static void main(String[] args) {
    Float floatObject2 = Float.valueOf(1.1);
    System.out.println(floatObject2);
    
  }
}

When compiling it, the compile generates error message:


Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method valueOf(String) in the type Float is not applicable for the arguments (double)

    at Main.main(Main.java:3)
Home 
  Java Book 
    Essential Classes  

Float:
  1. Float class
  2. MAX/MIN_VALUE Find out the Maximum value and Minimum value a float type can have
  3. Create a Float object
  4. Convert Float to byte, double, float, int, long and short
  5. Compare two float objects
  6. Infinite and Not A Number
  7. Convert float value to Hex String value
  8. Convert float value to String value
  9. Convert string value to float value
  10. Bit oriented calculation for float