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 .

Float Method Usage Examples

Modifier and Type Method and Description
byte byteValue()
This method just returns the equivalent of this Float object into byte after under going narrowing conversion since byte is of lower precision
static int compare(float f1, float f2)
Basically this method just Compares the two specified float values.
int compareTo(Float anotherFloat)
This is a handy addtion to Float methods which enable us to compare two Float object numerically
double doubleValue()
This method just returns the equivalent of this Float object into double primitive.
boolean equals(Object obj)
The equals method test if this Float is numerically equal to the Object method argument
static int floatToIntBits(float value)
Gets a representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout.
static int floatToRawIntBits(float value)
Returns a representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout, preserving Not-a-Number (NaN) values.
float floatValue()
This is basically just returns the primitive equivalent of this Float object.
int hashCode()
Returns a hash code for this Float object.
static int hashCode(float value)
Returns a hash code for a float value; compatible with Float.hashCode().
static float intBitsToFloat(int bits)
Returns the float value corresponding to a given bit representation.
int intValue()
This method just returns the equivalent of this Float object into primitive int after under going narrowing conversion since int is of lower precision.
static boolean isFinite(float f)
Returns true if the argument is a finite floating-point value; returns false otherwise (for NaN and infinity arguments).
boolean isInfinite(float v)
A value of true is being returned by this method if the specified number is infinitely large in magnitude, false otherwise.
boolean isNaN()
Returns true if this Float value is a Not-a-Number (NaN), false otherwise.
static boolean isNaN(float v)
Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
long longValue()
This method just returns the equivalent of this Float object into primitive long after under going narrowing conversion since float is of lower precision.
static float max(float a, float b)
The public static float max(float a, float b) method of Float class is use to get which of the two float is having the highest value. This method is a nice addition to Java API which has been recently added in Java 8.
static float min(float a, float b)
The public static float min(float a, float b) method of Float class is use to get which of the two float is having the lowest value. This method is a nice addition to Java API which has been recently added in Java 8
static float parseFloat(String s)
The Float.parseFloat(String s) java method is used primarily in parsing a String method argument into a Float object.
short shortValue()
This method just returns the equivalent of this Float object into primitive short after under going narrowing conversion since short is of lower precision.
static float sum(float a, float b)
The sum(float a, float b) method of Float class is very simple, it just returned the sum of the two floating point method argument a and b. Basically this is just another way of having the operation a + b.
static String toHexString(float f)
Returns a hexadecimal string representation of the float argument.
String toString()
Returns a string representation of this Float object.f
static String toString(float f)
Returns a string representation of the float argument.
static Float valueOf(float f)
Returns a Float instance representing the specified float value.
static Float valueOf(String s)
Returns a Float object holding the float value represented by the argument string s.