The Java Float Class is a crucial part of the Java programming language, offering a wide range of methods to handle the float data type efficiently. It helps developers write more maintainable and optimized code by providing essential utilities for floating-point arithmetic.
In this article, we will explore the key features, fields, constructors, and methods of the Float Class, offering a comprehensive understanding of its usage in various applications. Let’s dive into the details.
The Java Float class is a type of wrapper class. You might be thinking, what a wrapper class exactly is? We define a wrapper class as a particular class that contains a primitive data type in its object. And here in Float Class, the object will contain the data type “float” in its object.
Apart from this, the Float class has various practical methods for dealing with float variables. For example, it gives unique methods to compare two float values, a float value with a String data type, etc.
Now, let’s move forward and look at different constructors, methods and fields available in Java Float Class.
Are you eager to learn how to implement our first Float class program in Java? We've included a simple example below.
Java
Java
//java Float class equals() method import java.lang.Float; public class FloatClass { public static void main(String[] args) { Float val1 = 1.5f; Float val2 = new Float(1.5f); //display if the two values are equal if(val1.equals(val2)){ System.out.println("val1 and val2 are equal"); } else{ System.out.println("val1 and val2 are not equal"); } } }
You can also try this code with Online Java Compiler
Here, we have shown an example of the equals() method provided in Java Float Class, using which we can check if two float values are equal. Try and compile it on java compiler.
Fields of Float Class in Java
Let’s get started by understanding what does a field mean.
A "field" is a data container that contains information. It may contain the descriptive characteristics of the class. Let's look at the Float Class's fields and associated properties.
The fields associated with the Java.lang.Float Class are as follows:
Type
Field
Description
static int
MAX_EXPONENT
This is the highest possible exponent for a finite float variable.
static float
MAX_VALUE
This is a constant that holds the largest positive finite float value.
static int
MIN_EXPONENT
This is the smallest possible exponent for a normalized float variable.
static float
MIN_NORMAL
This is a constant that holds the smallest positive float normal value.
static float
MIN_VALUE
This is a constant that holds the smallest positive nonzero float value.
static float
NaN
This is a float constant that holds a Not-a-Number (NaN) value.
static float
NEGATIVE_INFINITY
This is a type of float constant that holds the negative infinity.
static float
POSITIVE_INFINITY
This is a type of float constant that holds the positive infinity.
static int
SIZE
This is the number of bits that we use to represent the float value.
static Class<Float>
TYPE
This is the instance of the class that represents the primitive type float.
Constructors of Java Float Class
We can create objects of the Float class using available constructors. The first constructor method uses a double value as a parameter, the second one uses a float value as a parameter, and the third one uses a String. The following constructors are available in the Float class:
Float(double value): Constructs a new Float object to reflect the argument that has been converted to float type.
Float(float value): The primitive float parameter is represented by a newly allocated Float object.
Float(String s): Constructs a new Float object to represent the floating-point value of the type float that the string represents.
Methods of Float Class in Java
The Java Float class has several methods that are particularly useful to programmers. We can use these methods to alter the float data type. The approaches are mentioned below. The uses and syntax of these methods are discussed for now, and in the end, we will implement all these methods in a program to get a clearer understanding.
1. toString()
To return a new String that represents the given double object, we use the toString() method. Take a look at the syntax in the example below to see how it works.
Syntax
public String toString(float b)
You can also try this code with Online Java Compiler
This method converts the specified string value to a float value and returns it. There is only one String type argument that it accepts. This method's syntax is listed below.
Syntax
public static double parseFloat(String val) throws NumberFormatException
You can also try this code with Online Java Compiler
The Float object retrieves a primitive type float value with this function. After conversion to type float, it returns the numeric value represented by this object. Let's look at the syntax in the example below to see how it works.
Syntax
public byte byteValue()
You can also try this code with Online Java Compiler
This method gives a true or false boolean value. If this Float value is a Not-a-Number (NaN), it returns true; otherwise, it returns false. Let’s look at the syntax of this method given below.
Syntax
public boolean isNaN()
You can also try this code with Online Java Compiler
This method determines whether a float value has an infinite magnitude. It returns either true or false as a boolean value. The following is the method's syntax.
Syntax
public boolean isInfinite()
You can also try this code with Online Java Compiler
We use this method to get the hexadecimal string representation of the float object. It accepts a float argument that is converted to a hexadecimal value. This method's syntax is as follows.
Syntax
public static String toHexString(float val)
You can also try this code with Online Java Compiler
This method returns the provided float argument’s IEEE 754 floating-point "single format" bit layout. A floating-point parameter is passed in this method. The syntax for this method is as follows.
Syntax
public static int floatToIntBits(float val)
You can also try this code with Online Java Compiler
We use IntBitsToFloat() method to generate the corresponding float value of the given long bit pattern as a parameter. A long type parameter is passed in this method. The method's syntax is shown below.
Syntax
public static int IntBitsToFloat(long val)
You can also try this code with Online Java Compiler
We use this method to get the provided float argument’s IEEE 754 floating-point "single format" bit layout. It differs from the “floatToIntBits()” method in that it preserves the NaN(Not a Number) values. A float type parameter is passed in this method. We have given the method's syntax below.
Syntax
public static int floatToRawIntBits(float val)
You can also try this code with Online Java Compiler
We use this method when we need to compare equality among two Float objects. If both objects have the same float value, this function returns true. Otherwise, it returns false. We pass an object as a parameter in this method. We've included the method's syntax below.
Syntax
public boolean equals(Object obj1)
You can also try this code with Online Java Compiler
We use this method to check for numerical equivalence between two primitive float values. Because it is a static method, we can use it without constructing a Float object.
The following section describes the method's syntax.
Syntax
public static int compare(float a, float b)
You can also try this code with Online Java Compiler
Let’s look at a program that implements various methods of Java Float Class.
Java
Java
//java float class compare() method import java.lang.Float; class FloatClass { public static void main(String args[]) { float f1=1.23456789f; float f2=1.23456789f; //compare two float values int result=Float.compare(f1,f2); System.out.println("Result of compare() method is "+result); //hashcode of float value int hashcode=Float.hashCode(f1); System.out.println("Hashcode of float value is "+hashcode); //parse float value float f3=Float.parseFloat("1.23456789"); System.out.println("Result of parseFloat() method is "+f3); //toString() method String str=Float.toString(f1); System.out.println("Result of toString() method is "+str); //valueOf() method float f4=Float.valueOf("1.23456789"); System.out.println("Result of valueOf() method is "+f4); //isNaN() method boolean b=Float.isNaN(f1); System.out.println("Result of isNaN() method is "+b); //isInfinite() method boolean b1=Float.isInfinite(f1); System.out.println("Result of isInfinite() method is "+b1); } }
You can also try this code with Online Java Compiler
Result of compare() method is 0
Hashcode of float value is 1067320914
Result of parseFloat() method is 1.2345679
Result of toString() method is 1.2345679
Result of valueOf() method is 1.2345679
Result of isNaN() method is false
Result of isInfinite() method is false
In the above program we have implemented the compare(), hashCode(), parseFloat(), toString(), valueOf(), isNaN() and isInfinite() methods. We would have to manually write codes to gain these functionalities without these methods. So, we can now understand the usefulness of Java Float Class methods.
Frequently Asked Questions
Why is float used in Java?
In Java, the default value for a float is 0.0f. When you wish to save memory, and your computations don't require more than 6 or 7 digits of precision, you should use the float data type.
What is double and float in Java?
In Java, float and double are data types used for storing decimal values. float is a 32-bit single-precision, while double is a 64-bit double-precision data type, offering more precision for larger decimal values.
What is float in data type?
The float data type is a 32-bit, single-precision number used to store decimal values. It is less precise than double and is commonly used when memory efficiency is prioritized over high precision in calculations.
What is the difference between “Float” and “float” in Java?
A “Float” is a reference type or a class defined in the standard library. It keeps track of a reference to an object with a value. A “float”, on the other hand, is a basic type that is built into the language.
Conclusion
In this article, we explored the fundamentals of the Java Float Class and covered the key topics, including its fields, constructors, and methods. These concepts provide a thorough understanding of how to effectively work with the float data type in Java.
We hope that this blog has helped you enhance your knowledge regarding Java Float Class and if you would like to learn more, check out our articles