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.


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/packages_api("`Packages / API`") 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/type_casting("`Type Casting`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/classes_objects -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/class_methods -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/modifiers -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/oop -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/packages_api -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/user_input -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/wrapper_classes -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/identifier -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/arrays -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/comments -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/data_types -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/operators -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/output -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/strings -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/type_casting -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/variables -.-> lab-117710{{"`Java Integer floatValue Method`"}} java/system_methods -.-> lab-117710{{"`Java Integer floatValue Method`"}} end

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