Google+

246. Float Wrapper Class







Pre-requisites -



Float wrapper class is used to wrap the float primitive type into an object, whenever we need primitive type to be used as an object.

Example of wrapping float primitive type into an object using Float wrapper class -

float pi = 3.14F;
Float p1 = new Float(pi);  //Wrapping the float primitive type variable pi  into Float wrapper class type object p1


Example of unwrapping the Float wrapper class type object p1 into float primitive type variable p -

float p = p1.floatValue( ) //Unwrapping the Float wrapper class type object p1 using floatValue( ) method to float primitive data type variable p

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'FloatWrapperClassDemo.java' with main( ) method in the existing Java Project 'Wrapper Classes' as shown below -



2. Declare a float primitive class type variable pi and assign it a value as shown below -


3. Now wrap the float primitive data type variable pi into a Float wrapper class type object p1 as shown below -



4. Now print the wrapped Float class type object 'p1' as shown below -


5. Save and Run the Java Class 'FloatWrapperClassDemo.java' and observe that the float  primitive data type value 3.14F is printed using the wrapped object 'p1' in the output as shown below -


6. Now unwrap the Float class object p1 back to float primitive data type as shown below -


7. Print the unwrapped float primitive data type 'p' as shown below - 


8. Save & Run the Java Class 'FloatWrapperClassDemo.java' and observe that the float primitive data type value 3.14F is printed using the unwrapped float primitive data type 'p' in the output as shown below -



Hence we can wrap the float primitive data type into an object using Float Wrapper Class and unwrap the object back to the float primitive data type using the floatValue( ) method.







Please comment below to feedback or ask questions.

Double Wrapper Class will be explained in the next post.








No comments: