Java Float floatToRawIntBits Method

JavaJavaBeginner
Practice Now

Introduction

The floatToRawIntBits() method is a static method in the Float class that returns the integer bits of a floating-point value in accordance with the IEEE 754 floating-point format.

Create the Java file

Firstly, let us create a new file named FloatToRawIntBits.java in the ~/project directory using the following command.

touch ~/project/FloatToRawIntBits.java

Write the import statements

In this step, you need to import the java.util and java.lang packages. Here's the code:

import java.util.*;
import java.lang.*;

Define the class

In this step, you need to define the FloatToRawIntBits class. Here's the code:

public class FloatToRawIntBits {
    public static void main(String[] args) {

    }
}

Use the floatToRawIntBits() method to convert floating values to integer bits

In this step, you will write code to convert floating values to integer bits using the floatToRawIntBits() method. Here is the code:

float n1 = 90.85f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n1));

float n2 = n1 / 0.0f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n2));

float n3 = -n1 / 0.0f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n3));

Float n4 = 0.0f / 0.0f;
System.out.println("Value in integer bits = " + Float.floatToRawIntBits(n4));

This code will first define some floating-point values (n1, n2, n3, and n4). The floatToRawIntBits() method is then called on each of these values to convert them to integer bits. The resulting integer bit values are then printed to the console using System.out.println().

Compile and run the code

In this step, you will compile and run the code that you wrote in the previous step. Open your terminal, navigate to the ~/project directory and type the following command.

javac FloatToRawIntBits.java && java FloatToRawIntBits

The output will be as shown below.

Value in integer bits = 1119204147
Value in integer bits = 2139095040
Value in integer bits = -8388608
Value in integer bits = 2143289344

Take user input

In this step, you will modify the code to take user input. Here is the code:

try {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a floating-point number: ");
    float f = sc.nextFloat();
    System.out.println("Value in integer bits = " + Float.floatToRawIntBits(f));
} catch (Exception e) {
    System.out.println("Invalid input!");
}

This code will use a Scanner object to get input from the user. It will prompt the user to enter a floating-point number, and then it will call the floatToRawIntBits() method to convert the input value to integer bits. If the user enters an invalid value, an error message will be printed to the console.

Compile and run the code

In this step, you will again compile and run the code that you wrote in the previous step. Open your terminal, navigate to the ~/project directory and type the following command.

javac FloatToRawIntBits.java && java FloatToRawIntBits

The output will be as follows:

Enter a floating-point number: 743.05
Value in integer bits = 1144636211

You can try with different input values and get the respective integer bits.

Modify the code to handle exceptions

In this step, you will modify the code to handle cases where the user enters an invalid input. Here is the updated code:

try {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter a floating-point number: ");
    float f = sc.nextFloat();
    System.out.println("Value in integer bits = " + Float.floatToRawIntBits(f));
} catch (InputMismatchException e) {
    System.out.println("Invalid input! Please enter a valid floating-point number.");
}

This code will catch InputMismatchException when invalid input is entered by the user. The catch block of the code will then print an error message to the console.

Compile and run the code

In this step, you will compile and run the updated code that you wrote in the previous step. Open your terminal, navigate to the ~/project directory and type the following command.

javac FloatToRawIntBits.java && java FloatToRawIntBits

The output will be as follows:

Enter a floating-point number: 0x699
Invalid input! Please enter a valid floating-point number.

This shows the error message when the user enters an invalid input.

Summary

In this lab, you learned about the floatToRawIntBits() method in the Java programming language. You learned how to use this method to convert floating-point values to integer bits, how to take user input, and how to handle exceptions. By completing this lab, you should now be able to use the floatToRawIntBits() method effectively in your own Java programs.

Other Java Tutorials you may like