Java.lang Package Classes

Java Float - valueOf() Method



The java.lang.Float.valueOf() method returns a Float instance representing the specified float value. If a new Float instance is not required, this method should generally be used in preference to the constructor Float(float), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Syntax

public static Float valueOf(float f)

Parameters

f Specify a float value.

Return Value

Returns a Float instance representing f.

Exception

NA.

Example:

In the example below, the java.lang.Float.valueOf() method returns a Float instance representing the given float value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating float value
    float x = 25.5f;

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

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

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

The output of the above code will be:

The float value is: 25.5
The Float instance is: 25.5

❮ Java.lang - Float

5