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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/classes_objects -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/class_methods -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/exceptions -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/modifiers -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/oop -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/user_input -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/wrapper_classes -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/identifier -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/arrays -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/data_types -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/operators -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/output -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/strings -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/variables -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} java/system_methods -.-> lab-117652{{"`Java Float floatToRawIntBits Method`"}} end

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