Java Float Class
The Float class wraps a value of primitive type float in an object. An object of type Float contains a single field whose type is float.
In addition, this class provides several methods for converting a float to a String and a String to a float, as well as other constants and methods useful when dealing with a float.
Declaration:
Below are the Methods of Float class and their example.
1. toString()
It returns a new String representing of specified float object. Syntax of the method is given below.
Syntax:
Example:
Lets take an example to get string object of a float type. We used toString()
method which is static so we can call it by using the class name.
Â
2. valueOf()
This method returns a Float instance representing the specified float value. This method should generally be used in preference to the constructor Float(float).
Syntax
Example:
In this example, we are using valueOf() method that returns instance of Float class which represents the specified float type.
Â
3. parseFloat()
This method returns a float value of the specified string value. We can use it to get a float value from string type value.
Syntax:
Example:
Lets take an example in which we have a string type variable and getting its float value using the parseFloat()
method.
Â
4. byteValue()
This method is used to get a primitive type float value from Float object. It returns the numeric value represented by this object after conversion to type float.
Syntax
5. shortValue()
This method returns the value of this Float as a short after a widening primitive conversion.
Syntax
6. intValue()
The intValue()
method returns the value of this Float as a primitive int type after a widening primitive conversion. Syntax of this method is given below.
Syntax
7. longValue()
The longValue()
method returns the value of this Float type as a long type after a widening primitive conversion. Syntax of this method is given below.
Syntax
8. doubleValue()
It returns the value of this Float type as a double type after a widening primitive conversion. Syntax of this method is given below.
Syntax
9. floatValue()
This method is used to get value of this Float type as a float type after a widening primitive conversion. Syntax of this method is given below.
Syntax:
Example:
Lets take an example to convert float type to int, long and float type values. In this example, we are using intValue(), floatValue(), doubleValue()
methods.
Â
10. hashCode()
This method is used to get hash code of a float value. It returns an int value of float object.
Syntax:
Example:
Â
11. isNaN()
This method returns a boolean value either true or false. It returns true, if this Float value is a Not-a-Number (NaN), false otherwise.
Syntax:
Example:
Lets take an example to check whether the given float value is NaN or not. See the below example.
Â
12. isInfinite()
This method is used to check whether the float value is infinitely large in magnitude. It returns a boolean value either true or false. Syntax of this method is given below.
Syntax:
Example:
We can use this method to check the range of Float value whether it lies under infinitely or not.
Â
13. toHexString()
This method is used to get a hexadecimal string representation of the float argument. It takes a float type argument that would be convert into hexadecimal value. Syntax of this method is given below.
Syntax:
Example:
Â
14. floatToIntBits()
This method is used to get representation of the specified floating-point value according to the IEEE 754 floating-point “single format” bit layout. It takes a floating-point argument. Syntax of this method is given below.
Syntax:
Example:
In this example, we are using floattointbits()
method that returns a bit layout of floating-point value.
Â
15. floatToRawIntBits()
This method 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. Syntax of this method is given below.
Syntax:
Example:
Â
16. IntBitsToFloat()
This method is used to get the float floating-point value with the same bit pattern. The argument is considered to be a representation of a floating-point value according to the IEEE 754 floating-point “single format” bit layout. Syntax of this method is given below.
Syntax:
Example:
Lets take an example to understand the intbitstofloat()
method that returns floating-point value.
Â
17. equals()
The equals()
method compares an object to the specified object. It returns true if objects are same; false otherwise. Syntax of this method is given below.
Syntax:
Example:
We are comparing two float objects using the equals method that returns true if both the objects are true.
Â
18. compareTo()
This method is used to compare two float objects numerically. It returns 0, if the both float objects are equal. It returns less the 0, if one float object is less than argument object. It returns greater than 0, if one float object is numerically greater than the argument float object. Syntax of this method is given below.
Syntax:
Example:
In this example, we are comparing two float objects using compareTo()
method that compares two float objects numerically and returns a numeric value.
Â
19. compare()
It is used to compare two float values numerically. The value returned is identical to what would be returned by. Syntax of this method is given below.
Syntax:
Example:
We can use compare method to compare two float values. It returns 0 if both are equal else returns either negative or positive numerical value.
Â
Â