Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Float Class
3.
Float Class Declaration
4.
Float Class Basic Implementation
4.1.
Output
5.
Fields of Float Class
6.
Constructors of Float Class
7.
Methods of Float Class
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
8.1.
Output
9.
FAQs
10.
Key Takeaways
Last Updated: Mar 27, 2024

Java Float Class

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

We come across various packages and classes while writing codes in Java. These help us to write more maintainable code. Among these classes, one of the interesting classes is “Java Float Class.” It offers many useful methods for dealing with a float data type.

This post will give you a clear understanding of Java Float Class and its different uses while dealing with the float data type. We will learn various fields, constructors and methods associated with the Float Class in Java.

I hope you will enjoy learning about the Java Float class. Let’s get started.

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

Java Float Class

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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>

Float Class Basic Implementation

Are you eager to learn how to implement our first Float class program in Java? We've included a simple example below.

//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");
        }
    }
}

Output

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

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:

Constructors of 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

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)

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)

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

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()

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()

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()

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()

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()

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()

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()

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()

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()

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)

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)

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)

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)

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)

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)

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)

Program of Float Class Methods

Let’s look at a program that implements various methods of Java Float Class.

//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);
    }
}

Output

 

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.

FAQs

  1. 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.
     
  2. Is float a Java keyword?
    The float keyword is a data type for fractional values in 3.4e038 to 3.4e+038.
     
  3. Is Float is a class in Java?
    Yes. It is a wrapper class for float primitive values. The Float class provides several helpful methods for working with float values.
     
  4. 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.

Key Takeaways

In this article, we have covered all the fundamentals of Java Float Class and went through the following topics:

  • Float Class Declaration
  • Float Class Basic Implementation
  • Fields of Float Class
  • Constructors of Float Class
  • Methods of Float Class
  • A program demonstrating various methods of Float Class

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 on “Program to count the total number of characters in the string” and “selenium interview questions.” Do upvote our blog to help other ninjas grow.

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

Happy Reading!

Live masterclass