Java.lang Package Classes

Java Float - valueOf() Method



The java.lang.Float.valueOf() method returns a Float object holding the float value represented by the argument string s. If s is null, then a NullPointerException is thrown.

Leading and trailing whitespace characters in s are ignored. Whitespace is removed as if by the String.trim() method; that is, both ASCII space and control characters are removed. The rest of s should constitute a FloatValue as described by the lexical syntax rules.

Syntax

public static Float valueOf(String s)
                     throws NumberFormatException

Parameters

s Specify the string to be parsed.

Return Value

Returns a Float object holding the value represented by the String argument.

Exception

Throws NumberFormatException, if the string does not contain a parsable number.

Example:

In the example below, the java.lang.Float.valueOf() method returns a Float object holding the value given by the specified String.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating a string holding float value
    String x = "25";

    //creating Float object
    Float y = Float.valueOf(x);

    //printing the string
    System.out.println("The string is: " + x); 

    //printing the Float object 
    System.out.println("The Float object is: " + y);   
  }
}

The output of the above code will be:

The string is: 25
The Float object is: 25.0

❮ Java.lang - Float