java.lang.Float Class in java
java.lang.Float
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.
Creating Objects
Using the Float constructor:
Float(float value):
Creates a Float object initialized with the specified float value.
Float(String s):
Parses a string representation of a floating-point number and creates a Float object with the corresponding value.
// Creating a Float object from a float value
float distance = 10.5f;
Float distanceObject = new Float(distance);
Float Class Methods
public float floatValue():
Returns the primitive float value stored in the Float object.
public static Float valueOf(String s):
Parses a string and returns a new Float object with the equivalent value. (Similar to the constructor, but returns a Float object)
public static float parseFloat(String s):
Parses a string and returns the primitive float value it represents.
public static boolean isNaN(float v):
Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
public static boolean isInfinite(float v):
Returns true if the specified number is infinitely large in magnitude, false otherwise.
public static boolean isFinite(float f):
Returns true if the argument is a finite floating-point value; returns false otherwise (for NaN and infinity arguments).
public boolean isNaN():
Returns true if this Float value is a Not-a-Number (NaN), false otherwise.
public boolean isInfinite():
Returns true if this Float value is infinitely large in magnitude, false otherwise.
Float Class Example
import java.lang.Float;
public class FloatExample {
public static void main(String[] args) {
// Creating Float objects
Float float1 = new Float(3.14f);
Float float2 = new Float("123.45");
// Displaying the values
System.out.println("Float 1: " + float1);
System.out.println("Float 2: " + float2);
// Using Float methods
float floatValue1 = float1.floatValue();
float floatValue2 = float2.floatValue();
System.out.println("Float 1 as float: " + floatValue1);
System.out.println("Float 2 as float: " + floatValue2);
// Comparing Float objects
int comparison = Float.compare(float1, float2);
System.out.println("Comparison result: " + comparison);
// Checking for NaN and Infinity
Float floatNaN = Float.NaN;
Float floatInfinity = Float.POSITIVE_INFINITY;
System.out.println("Is floatNaN NaN? " + floatNaN.isNaN());
System.out.println("Is floatInfinity finite? " + floatInfinity.isInfinite());
// Parsing a float from a string
String floatString = "78.9";
float parsedFloat = Float.parseFloat(floatString);
System.out.println("Parsed float: " + parsedFloat);
// Converting a float to String representation
String floatAsString = Float.toString(56.78f);
System.out.println("Float as string: " + floatAsString);
// Getting the hash code of a Float object
int hashCode = float1.hashCode();
System.out.println("Hash code of Float 1: " + hashCode);
}
}
Output
Float 1: 3.14
Float 2: 123.45
Float 1 as float: 3.14
Float 2 as float: 123.45
Comparison result: -1
Is floatNaN NaN? true
Is floatInfinity finite? true
Parsed float: 78.9
Float as string: 56.78
Hash code of Float 1: 1078523331