• 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

  1. public final class Float
  2. extends Number
  3. implements Comparable<Float>

Float Class Constructors

1.public Float(double value)


  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Float f = new Float(22.56d);
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56


2.public Float(float value)


  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Float f = new Float(22.56f);
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 22.56


3.public Float(String s) throws NumberFormatException


  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  

  7.  Float f = new Float("22.56f");
  8.   System.out.println(f);
  9.  
  10.  
  11. }
  12. }

Output:
  1. 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 

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatValueOfDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="56.32";

  8.  Float f = Float.valueOf(str);
  9.   System.out.println(f);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 56.32

2.public String toString()
  • This method returns String value from Float Object.

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatValueOfDemo {
  4.  
  5. public static void main(String[] args) {
  6.   
  7.  Float f = new Float("12.3")
  8.  String str=f.toString();

  9.   System.out.println(str);
  10.  
  11.  
  12. }
  13. }



Output:
  1. 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 

  1. package com.instanceofjavatutorial;
  2.  
  3. public class FloatDemo {
  4.  
  5. public static void main(String[] args) {
  6.  
  7.  String str="20";

  8.  Float f = Float.parseFloat(str);
  9.   System.out.println(f);
  10.  
  11.  
  12. }
  13. }

Output:
  1. 20.0



Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu