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
Getting a Float Object from a float value Carring Positive Value Example
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
Getting a Float Object from a float value Carring Negative Value Example
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
Getting a Float Object from a float value Carring Negative Zero Value Example
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
Getting a Float Object from a float value Carring Positive Zero Value Example
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