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.

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