Java Integer floatValue Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the Java floatValue() method of the Integer class, which is used for converting an Integer object into its float equivalent. You will also learn about the syntax, parameters, and return value of this method.

Create a Java file

Create a Java file named IntegerFloatValueLab.java in the ~/project/ directory using the following command:

touch ~/project/IntegerFloatValueLab.java

Write code to convert Integer to float

In the IntegerFloatValueLab.java file, write the following Java code to convert an Integer object to its float equivalent using the floatValue() method:

import java.util.Scanner;

public class IntegerFloatValueLab {
      public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);

         // Take integer input from user
         System.out.print("Enter an integer value: ");
         int num = sc.nextInt();

         // Convert Integer to float
         Integer integerObj = num;
         float floatNum = integerObj.floatValue();

         // Print the float value
         System.out.println("Float value of " + num + " is: " + floatNum);

         sc.close();
      }
}

In this code, we take an integer value as input from the user using the Scanner class. The input value is then converted to an Integer object using autoboxing. Finally, we use the floatValue() method to convert the Integer object to its float equivalent and print the result.

Compile and run the Java code

Compile and run the Java code using the following commands in the terminal:

javac ~/project/IntegerFloatValueLab.java
java IntegerFloatValueLab

This will compile and run the Java code. You should see the following output in the terminal:

Enter an integer value: 25
Float value of 25 is: 25.0

Modify the Java code

Modify the Java code by adding the following lines at the end of the main() method to convert the float value back to an Integer object and print the result:

// Convert float to Integer
Integer intValue = (int) floatNum;

// Print the Integer value
System.out.println("Integer value of " + floatNum + " is: " + intValue);

The complete code should look like this:

import java.util.Scanner;

public class IntegerFloatValueLab {
      public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);

         // Take integer input from user
         System.out.print("Enter an integer value: ");
         int num = sc.nextInt();

         // Convert Integer to float
         Integer integerObj = num;
         float floatNum = integerObj.floatValue();

         // Print the float value
         System.out.println("Float value of " + num + " is: " + floatNum);

         // Convert float to Integer
         Integer intValue = (int) floatNum;

         // Print the Integer value
         System.out.println("Integer value of " + floatNum + " is: " + intValue);

         sc.close();
      }
}

Compile and run the modified Java code

Compile and run the modified Java code using the same commands as before in the terminal:

javac ~/project/IntegerFloatValueLab.java
java IntegerFloatValueLab

This will compile and run the modified Java code. You should see the following output in the terminal:

Enter an integer value: 100
Float value of 100 is: 100.0
Integer value of 100.0 is: 100

Summary

Congratulations, you have completed the Java Integer floatValue() Method Lab! In this lab, you learned about the floatValue() method of the Integer class and how it can be used to convert an Integer object to its float equivalent. You also learned how to write, compile, and run Java code in the terminal and how to test your code using different input values.

Other Java Tutorials you may like