Java - Float valueOf(float) method



Description

The Java Float valueOf(float f) method returns a Float instance representing the specified float value f.

Declaration

Following is the declaration for java.lang.Float.valueOf() method

public static Float valueOf(float f)

Parameters

f − This is the float value.

Return Value

This method returns a a Float instance representing f.

Exception

NA

Example 1

The following example shows the usage of Float valueOf() method to get a Float object of a float value. We've initialized one Float object with a positive float using valueOf() method. Then we're printing the string representation value of the object.

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {
      Float f1 = Float.valueOf(6.5f);
   
      // print the Float instance representing the specified float value 
      System.out.println("Value = " +  f1);
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Value = 6.5

Example 2

The following example shows the usage of Float valueOf() method to get a Float object of a float value. We've initialized one Float object with a negative float using valueOf() method. Then we're printing the string representation value of the object.

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {
      Float f1 = Float.valueOf(-6.5f);
   
      // print the Float instance representing the specified float value 
      System.out.println("Value = " +  f1);
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Value = -6.5

Example 3

The following example shows the usage of Float valueOf() method to get a Float object of a float value. We've initialized one Float object with a negative zero float using valueOf() method. Then we're printing the string representation value of the object.

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {
      Float f1 = Float.valueOf(-0.0f);
   
      // print the Float instance representing the specified float value 
      System.out.println("Value = " +  f1);
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Value = -0.0

Example 4

The following example shows the usage of Float valueOf() method to get a Float object of a float value. We've initialized one Float object with a positive zero float using valueOf() method. Then we're printing the string representation value of the object.

package com.tutorialspoint;
public class FloatDemo {
   public static void main(String[] args) {
      Float f1 = Float.valueOf(0.0f);
   
      // print the Float instance representing the specified float value 
      System.out.println("Value = " +  f1);
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Value = 0.0
java_lang_float.htm
Advertisements