Java Float valueOf() Method

The valueOf() method of Java Float class returns a float instance representing the specified float value 'f' or string argument 's'.

Syntax:

Parameters:

  • Float type (f) parameter is passed.
  • String type(s) parameter is passed.

Return Value:

The valueOf() method returns the float object of the specified float or String value passed.

Example 1

Output:

1. Value = 4.8654684E7
2. Value = -6789.87
3. Value = 0.0988767

Example 2

Output:

Enter your pin code : 121002
121002.0
Area = Faridabad.

Example 3

Output:

1. Value = Infinity
2. Value = -Infinity
3. Value = 1.17549435E-38
4. Value = 3.4028235E38
5. Value = 1.4E-45
6. Value = NaN

Example 4

Output:

Enter string value: reema
Exception in thread "main" java.lang.NumberFormatException: For input string: "reema"
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
	at sun.misc.FloatingDecimal.parseFloat(FloatingDecimal.java:122)
	at java.lang.Float.parseFloat(Float.java:451)
	at java.lang.Float.valueOf(Float.java:416)
	at Float_valueOfMethodExample4.main(Float_valueOfMethodExample4.java:8)

It will give us run time error because string value reema can't be converted to Float, so this run time exception should be checked.