Java Float floatValue Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the floatValue() method of the Float class in Java. The floatValue() method is used to convert a Float object to its corresponding float primitive value. This lab will guide you through the process of using this method with the help of code examples.


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/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/comments("`Comments`") 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/string_methods("`String Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117654{{"`Java Float floatValue Method`"}} java/classes_objects -.-> lab-117654{{"`Java Float floatValue Method`"}} java/class_methods -.-> lab-117654{{"`Java Float floatValue Method`"}} java/modifiers -.-> lab-117654{{"`Java Float floatValue Method`"}} java/oop -.-> lab-117654{{"`Java Float floatValue Method`"}} java/user_input -.-> lab-117654{{"`Java Float floatValue Method`"}} java/wrapper_classes -.-> lab-117654{{"`Java Float floatValue Method`"}} java/identifier -.-> lab-117654{{"`Java Float floatValue Method`"}} java/arrays -.-> lab-117654{{"`Java Float floatValue Method`"}} java/comments -.-> lab-117654{{"`Java Float floatValue Method`"}} java/data_types -.-> lab-117654{{"`Java Float floatValue Method`"}} java/operators -.-> lab-117654{{"`Java Float floatValue Method`"}} java/output -.-> lab-117654{{"`Java Float floatValue Method`"}} java/strings -.-> lab-117654{{"`Java Float floatValue Method`"}} java/variables -.-> lab-117654{{"`Java Float floatValue Method`"}} java/string_methods -.-> lab-117654{{"`Java Float floatValue Method`"}} java/system_methods -.-> lab-117654{{"`Java Float floatValue Method`"}} end

Create a Java file

Open the terminal and create a new Java file using the following command:

touch FloatValueLab.java

Define the Main Class

In order to use the Float class and its floatValue() method, we need to define a main class. Enter the following code in the FloatValueLab.java file:

public class FloatValueLab {
    public static void main(String[] args) {
        // code goes here
    }
}

Create Float Objects

In this step, we will create two Float objects with the help of the valueOf() method. Enter the following code inside the main method:

Float f1 = Float.valueOf("3.14");
Float f2 = Float.valueOf("5.67");

The valueOf() method accepts a String argument and returns a Float object corresponding to the value of the argument.

Convert Float Objects to float

Now, we will use the floatValue() method to convert the Float objects to their corresponding primitive float values. Enter the following code inside the main method:

float x = f1.floatValue();
float y = f2.floatValue();

The floatValue() method is an instance method of the Float class that returns the float value of the Float object.

Print the float values

Finally, we will print the float values to the console. Enter the following code inside the main method:

System.out.println("Value of f1 as float: " + x);
System.out.println("Value of f2 as float: " + y);

Compile and Run the Java program

Save the FloatValueLab.java file and compile it using the following command:

javac FloatValueLab.java

Once the compilation is successful, execute the program by typing the following command:

java FloatValueLab

Provide input through keyboard

Now, we will write a program to take user input through the console and convert it to the corresponding float value. Update the main method with the following code:

Scanner sc = new Scanner(System.in);

System.out.print("Enter a float value: ");
String input = sc.nextLine();

Float f = Float.valueOf(input);

System.out.println("Value of input as float: " + f.floatValue());

In this code, we first create a Scanner object to read input from the console. We then prompt the user to enter a float value and store the input in a String variable named input. We then convert this String value to a Float object using the valueOf() method. Finally, we use the floatValue() method to get the corresponding float primitive value and print it to the console.

Compile and Run the Java program

Save the FloatValueLab.java file and compile it using the following command:

javac FloatValueLab.java

Once the compilation is successful, execute the program by typing the following command:

java FloatValueLab

This will prompt you to enter a float value. Enter a value of your choice and press enter. The program will output the corresponding float value.

Summary

In this lab, you learned how to use the floatValue() method of the Float class in Java. You learned how to create Float objects using the valueOf() method and how to convert them to their corresponding primitive float values using the floatValue() method. You also learned how to read user input from the console and convert it to a float value using the Float class methods.

Other Java Tutorials you may like