- Float class is presented in java.lang package
- java.lang.Float class is used to represent primitive float value to Float object.
Float Class Definition
- public final class Float
- extends Number
- implements Comparable<Float>
Float Class Constructors
1.public Float(double value)
- package com.instanceofjavatutorial;
- public class FloatDemo {
- public static void main(String[] args) {
- Float f = new Float(22.56d);
- System.out.println(f);
- }
- }
Output:
- 22.56
2.public Float(float value)
- package com.instanceofjavatutorial;
- public class FloatDemo {
- public static void main(String[] args) {
- Float f = new Float(22.56f);
- System.out.println(f);
- }
- }
Output:
- 22.56
3.public Float(String s) throws NumberFormatException
- package com.instanceofjavatutorial;
- public class FloatDemo {
- public static void main(String[] args) {
- Float f = new Float("22.56f");
- System.out.println(f);
- }
- }
Output:
- 22.56
Float Class Methods:
1.public static Float valueOf(String s) throws NumberFormatException
- This method used to convert string value to float value. If the string contains non parsable value then it throws NumberFormatException
- package com.instanceofjavatutorial;
- public class FloatValueOfDemo {
- public static void main(String[] args) {
- String str="56.32";
- Float f = Float.valueOf(str);
- System.out.println(f);
- }
- }
Output:
- 56.32
2.public String toString()
- This method returns String value from Float Object.
- package com.instanceofjavatutorial;
- public class FloatValueOfDemo {
- public static void main(String[] args) {
- Float f = new Float("12.3")
- String str=f.toString();
- System.out.println(str);
- }
- }
Output:
- 12.3
3.public static float parseFloat(String s) throws NumberFormatException
- This method returns float value from string object. If string doesnt contains parsable float value then throws NumberFormatException
- package com.instanceofjavatutorial;
- public class FloatDemo {
- public static void main(String[] args) {
- String str="20";
- Float f = Float.parseFloat(str);
- System.out.println(f);
- }
- }
Output:
- 20.0
No comments