java.lang.Float

The Float class simply a wrapper class for the primitive type float. It wraps the float primitive value to an object. An object of type Float contains a single field whose type is float. So what would be the benefit if wraps around the primitive data type to an object? Well basically we would be able to access all the methods that is readily available on the Float class which is basically useful. We will go over each and every method available to Float class thoroughly.

Float Class Syntax

public final class Float
extends Number
implements Comparable<Float>

Float Compatibility Version

Float Class is available since JDK 1.0

Float Basic Usage

The Float class as part of the java.lang package is one of the classes of the java api that is being widely used. Let’s tackle the basics of using the Float class. Let’s discuss first how to instantiate a Float object

Basically the declaration of a Float object is almost the same as we are doing in assigning primitive data type float. A character f and F is still appended at the end of the numeric value to signify that this value is of primitive type float. This is a straightforward method to instantiate a Float object type. Let’s take another way to do this.

The 2nd method is using the autoboxing concept of java programming language which directly converts a primitive data type to its corresponding wrapper class.  If you would like to go more into details, I am recommending for you to read the official java documentation regarding autoboxing and unboxing.

There are three constructor available to Float class which we can use to instantiate a Float object:

Constructor Description
Float(double value) Constructs a newly allocated Float object that represents the argument converted to type float.
Float(float value) Constructs a newly allocated Float object that represents the primitive float argument.
Float(String s) Constructs a newly allocated Float object that represents the floating-point value of type float represented by the string.

 

We already discussed on how to use the Float(float value) in instantiating a Float object. So basically the other constructors usage is almost the same only in these cases we are accepting other primitive data type other than float.

Float Test of Equality

Normally when we do equality test for float primitive data type we do like this:

This will no longer work in dealing with Float object type. To test equality we need to make use of equals which is a method inherited from Object class.

 

Maximum and Minimum Value of Float

To find the maximum and minimum value of Float , we would be invoking two if it’s fields. Maximum value of an Float object is 3.4028235E38 which can be found using

and the minimum value of Float is 1.4E-45 which can be found also using one of the fields of Float

So why do we care about these values? First reason is to know the limit on what value should we assign to our Float object. Secondly if we go through on the methods of the Float class we would find some parsing and conversion from another object type to Float type. Lets try to take a look on the parsefloat(String s) method which take an argument string and convert it into Float object. So what if we pass a string value which has a float equivalent that is higher than the maximum value as defined by the Float.MAX_VALUE? To have a birds eye view of this scenario lets take a look on below example:

As you would have noticed we have passed a method argument to parseint method that is greater than maximum value which certainly breached the threshold. So if we try to run the above code snippet, the value printed will be Infinity .