Table of contents
1.
Introduction
2.
What is Float Class in Java?
3.
Java Float Class Declaration
4.
Implementation of Float Class in Java
4.1.
Java
5.
Fields of Float Class in Java
6.
Constructors of Java Float Class
7.
Methods of Float Class in Java
7.1.
1. toString()
7.1.1.
Syntax
7.2.
2. valueOf()
7.2.1.
Syntax
7.3.
3. parseFloat()
7.3.1.
Syntax
7.4.
4. byteValue()
7.4.1.
Syntax
7.5.
5. shortValue()
7.5.1.
Syntax
7.6.
6. intValue()
7.6.1.
Syntax
7.7.
7. longValue()
7.7.1.
Syntax
7.8.
8. doubleValue()
7.8.1.
Syntax
7.9.
9. floatValue()
7.9.1.
Syntax
7.10.
10. hashCode()
7.10.1.
Syntax
7.11.
11. isNaN()
7.11.1.
Syntax
7.12.
12. isInfinite()
7.12.1.
Syntax
7.13.
13. toHexString()
7.13.1.
Syntax
7.14.
14. floatToIntBits()
7.14.1.
Syntax
7.15.
15. IntBitsToFloat()
7.15.1.
Syntax
7.16.
16. floatToRawIntBits()
7.16.1.
Syntax
7.17.
17. equals()
7.17.1.
Syntax
7.18.
18. compareTo()
7.18.1.
Syntax
7.19.
19. compare()
7.19.1.
Syntax
8.
Program of Float Class Methods in Java
8.1.
Java
8.2.
Output
9.
Frequently Asked Questions
9.1.
Why is float used in Java?
9.2.
What is double and float in Java?
9.3.
What is float in data type?
9.4.
What is the difference between “Float” and “float” in Java?
10.
Conclusion
Last Updated: Sep 9, 2024
Easy

Java Float Class

Introduction

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. 

Java Float Class

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.

Also see, Duck Number in Java and Hashcode Method in Java

What is Float Class in Java?

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.
 

Also see,  Swap Function in Java

Java Float Class Declaration

We'll look at how a Float class is implemented in Java in this section. 
An example of a Float class declaration code is as follows:

public final class Float extends Number
implements Comparable<Float>
You can also try this code with Online Java Compiler
Run Code

Implementation of Float Class in Java

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
Run Code

Output

val1 and val2 are equal

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:

TypeFieldDescription
static intMAX_EXPONENTThis is the highest possible exponent for a finite float variable.
static floatMAX_VALUEThis is a constant that holds the largest positive finite float value.
static intMIN_EXPONENTThis is the smallest possible exponent for a normalized float variable.
static floatMIN_NORMALThis is a constant that holds the smallest positive float normal value.
static floatMIN_VALUEThis is a constant that holds the smallest positive nonzero float value.
static floatNaNThis is a float constant that holds a Not-a-Number (NaN) value.
static floatNEGATIVE_INFINITYThis is a type of float constant that holds the negative infinity.
static floatPOSITIVE_INFINITYThis is a type of float constant that holds the positive infinity.
static intSIZEThis is the number of bits that we use to represent the float value.
static Class<Float>TYPEThis 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
Run Code

2. valueOf()

This function returns a Float instance representing the float or string value provided. To show how it works, look at the syntax below.

Syntax

public Static Float valueOf(float b)
You can also try this code with Online Java Compiler
Run Code

3. parseFloat()

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
Run Code

4. byteValue()

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
Run Code

5. shortValue()

This method returns the value of this Float as a short (after casting to short type). Take a look at the syntax below to see how it works.

Syntax

public short shortValue()
You can also try this code with Online Java Compiler
Run Code

6. intValue()

This method returns int as the value of the provided Float by casting to int type. To understand how it works, look at the syntax below.

Syntax

public int intValue()
You can also try this code with Online Java Compiler
Run Code

7. longValue()

The given float object returns a long value by casting to a long type. Have a look at the syntax below to see how it functions.

Syntax

public long longValue()
You can also try this code with Online Java Compiler
Run Code

8. doubleValue()

Following a widening primitive conversion, it returns the Float type's value as a double type. The syntax for this method is as follows.

Syntax

public double doubleValue()
You can also try this code with Online Java Compiler
Run Code

9. floatValue()

The float value of this Float object is returned by this method. The syntax for this method is as follows:

Syntax

public float floatValue()
You can also try this code with Online Java Compiler
Run Code

10. hashCode()

We use this method to get the hash code of a float object. This method returns an int value. Below is the syntax of the hashCode() method.

Syntax

public int hashCode()
You can also try this code with Online Java Compiler
Run Code

11. isNaN()

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
Run Code

12. isInfinite()

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
Run Code

13. toHexString()

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
Run Code

14. floatToIntBits()

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
Run Code

15. IntBitsToFloat()

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
Run Code

16. floatToRawIntBits()

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
Run Code

17. equals()

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
Run Code

18. compareTo()

We use this method when two Float objects are being compared for numerical equality. 

→ If both objects are equal, it returns 0.

→ If one Float object is less than the argument, the result is less than 0.

→ Otherwise, it returns a value greater than zero.

The method's syntax is provided in the following section.

Syntax

public int compareTo(Float b)
You can also try this code with Online Java Compiler
Run Code

19. compare()

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
Run Code

Program of Float Class Methods in Java

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
Run Code

Output

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?

“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

 “Program to count the total number of characters in the string” 

selenium interview questions.”

Head over to our practice platform Code360 to practice top problems, attempt mock tests, read interview experiences, and much more.!

Live masterclass