Advertisement



< Prev
Next >



Float Wrapper Class





Float wrapper class is used to create an object version of a primitive float value.




    • Constructor of Float wrapper class.

    • Constructor Description
      Float(double d) Constructor of Float wrapper class takes a primitive double value.
      Float(float f) Constructor of Float wrapper class also takes a primitive float value.
      Float(String str) Constructor of Float wrapper class also takes a String equivalent of a floating point value.





      Note :


      A primitive int value can also be passed to constructor of Float wrapper class, because the conversion of an int to float does not result in any loss in the value of int.




    • Creating Float objects.

    • class A
      {
      public static void main(String... ar)
      {
      
      Float b1 = new Float(10.5); 		//Boxing a primitive float to Float object.
      Float b2 = new Float("20.0"); 		//Passing a primitive float value as a String
      Float b3 = new Float(30);	  	//Passing a primitive int value to Constructor of Float class
      }
      }





    • Reading the Float wrapper class object.

      There are two ways to read a Float object-
    • By directly printing the Float object.
    • Calling method, floatValue() of Float wrapper class, which method returns the primitive float equivalent.
    class A
    {
    public static void main(String... ar)
    {
    Float b1 = new Float(10.5); 	//Boxing a primitive double to a Float object.
    Float b2 = new Float("20"); 	//Passing primitive int as a String.
    
    //Unboxing a Float object.
    System.out.println(b1);	
    
    
    //Unboxing the Float object using floatValue() method
    System.out.println(b2.floatValue());
    }
    }


    Output-


    10.5
    20.0



    Advertisement




    Some important methods of Float wrapper class


    Methods Description
    int compareTo(Float b) - Returns a zero if the invoked Float object is same as value of b.
    - Returns a positive value if the invoked Float object is > b.
    - Returns a negative value if the invoked Float object is < b.
    boolean equals(Object ob) Returns a true if invoked Float object has same value as referred by ob, else false.
    static float parseFloat(String s) Returns a primitive float value if String, s could be converted to a valid float value.
    static Float valueOf(Float b) Returns a Float object after converting it from primitive float value, b.
    static Float valueOf(String s) Returns a Float object after converting it from a String, s.
    short shortValue() Converts a Float object to a primitive short value and returns it
    byte byteValue() Converts a Float object to a primitive byte value and returns it
    int intValue() Converts a Float object to a primitive int value and returns it.
    long longValue() Converts a Float object to a primitive long value and returns it
    float floatValue() Converts a Float object to a primitive float value and returns it
    double doubleValue() Converts a Float object to a primitive double value and returns it.





    Using compareTo() method to compare values in two Float objects.


    Method compareTo(Float f) takes Float class object and it -
    • Returns a zero if the invoked Float object contains the value same as f.
    • Returns 1 if the invoked Float object contains value larger than f.
    • Returns -1 if the invoked Float object contains value smaller than f.
    class A
    {
    public static void main(String... ar)
    {
    Float f1 = new Float("10.5"); 		//Constructor accepting String value
    Float f2 = new Float(10.5);	  	//Constructor accepting primitive float value
    
    System.out.println("Value in b1 = "+f1);
    System.out.println("Value in b2 = "+f2);
    
    System.out.println("Invoking f1 to compare with f2 : "+ f1.compareTo(f2));
    
    Float f3 = new Float("20.5");
    Float f4 = new Float(15.5);
    
    System.out.println("Value in f3 = "+f3);
    System.out.println("Value in f4 = "+f4);
    
    System.out.println("Invoking f3 to compare with f4 : "+ f3.compareTo(f4));
    
    System.out.println("Invoking b4 to compare with f3 : "+ f4.compareTo(f3));
    
    }
    }


    Output-


    Value in b1 = 10.5
    Value in b2 = 10.5
    Invoking f1 to compare with f2 : 0
    Value in f3 = 20.5
    Value in f4 = 15.5
    Invoking f3 to compare with f4 : 1
    Invoking b4 to compare with f3 : -1





    parseFloat() method of Float class


    Method parseFloat(), takes a string value and parse it to a primitive float value.
    //Wrapper class Float
    
    import java.util.*;
    
    class A
    {
    public static void main(String... ar)
    {
    float f1 = Float.parseFloat("5");
    float f2 = Float.parseFloat("-100.6");
    
    System.out.println("Primitive float value in f1 : "+ f1);
    System.out.println("Primitive float value in f2 : "+ f2);
    }
    }
    


    Output-


    Primitive float value in f1 : 5.0
    Primitive float value in f2 : -100.6





    Exception when converting a String to an Float object or float primitive value.


      NumberFormatException is raised when we pass String value to the constructor of Float class or its method parseFloat(), which is not a legal float value or it is out-of-range.
      //Converting String to object of Float class by passing it to constructor of Float class.
      
      class A
      {
      public static void main(String... ar)
      {
      Float b1 = new Float("100t"); //Passing a String value that cannot be convered to a Float object.
      }
      }
      


      Output -


      Exception in thread "main" java.lang.NumberFormatException: For input string: "100t"
              at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
              at sun.misc.FloatingDecimal.parseFloat(Unknown Source)
              at java.lang.Float.parseFloat(Unknown Source)
              at java.lang.Float.(Unknown Source)
              at A.main(Float5.java:7)





      Exception when converting String to primitive float by calling parseFloat().


      //Converting String to primitive float value by passing it to parseFloat() method of Float class.
      
      class A
      {
      public static void main(String... ar)
      {
      float f = Float.parseFloat("27.5ff"); //Passing a String value that can't be converted to float value.
      }
      }
      


      Output -


      Exception in thread "main" java.lang.NumberFormatException: For input string: "27.5ff"
              at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
              at sun.misc.FloatingDecimal.parseFloat(Unknown Source)
              at java.lang.Float.parseFloat(Unknown Source)
              at A.main(Float6.java:7)





      Converting the value stored in a Byte object to all numeric primitive types.


      import java.util.*;
      
      class A
      {
      public static void main(String... ar)
      {
      Float f = new Float(19.0);	//Converting an int value argumet to wrapper Float object
      
      System.out.println("Value in wrapped object, f : "+ f);
      
      byte b = f.byteValue();		//Returns a primitive byte value out of a wrapped Float object
      short s= f.shortValue();	//Returns a primitive short value out of a wrapped Float object
      int i = f.intValue();		//Returns a primitive int value out of a wrapped Float object
      long l = f.longValue();		//Returns a primitive long value out of a wrapped Float object
      float f = f.floatValue();	//Returns a primitive float value out of a wrapped Float object
      double d = f.doubleValue();	//Returns a primitive double value out of a wrapped Float object
      
      System.out.println("byte value of Float object, f : " + b);
      System.out.println("short value of Float object, f : " + s);
      System.out.println("int value of Float object, f : " + i);
      System.out.println("long value of Float object, f : " + l);
      System.out.println("float value of Float object, f : " + f);
      System.out.println("double value of Float object, f : "+ d);
      
      }
      }
      


      Output-


      Value in wrapped Float object, f : 19.0
      byte value of Float object, f : 19
      short value of Float object, f : 19
      int value of Float object, f : 19
      long value of Float object, f : 19
      float value of Float object, f : 19.0
      double value of Float object, f : 10.0




      Please share this article -







      Advertisement

  • Please Subscribe

    Please subscribe to our social media channels for daily updates.


    Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




    Advertisement

    Notifications



    Please check our latest addition

    PYTHON and DJANGO


    Advertisement