< Prev
Next >



Float Wrapper Class





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





  • 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





    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 -
    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.


  • Please subscribe our social media channels for notifications, we post a new article everyday.

    Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

    Coming Next
    -
    C++ and Python

    Ad2