Java Integer ValueOf Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn about the Integer.valueOf(int i) method in Java. This method is part of the java.lang package and is used to return the equivalent Integer object of the integer primitive value passed as an argument.


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

Creating a Java Class

We will start by creating a Java class in the ~/project directory. Open the terminal and navigate to the project directory, then create a new file called IntegerValueOf.java. Type the following command in the terminal:

touch IntegerValueOf.java

This command will create an empty Java file called IntegerValueOf.java.

Importing Required Package

The Integer class is part of the java.lang package, which is imported by default in Java. However, we need to explicitly import the Scanner class from the java.util package to take input from the user. Add the following lines of code at the beginning of the IntegerValueOf.java file:

import java.lang.Integer;
import java.util.Scanner;

Creating Main Method

In Java, execution of the program starts with the main method. We will create the main method as follows:

public class IntegerValueOf {
    public static void main(String[] args) {
        // code block
    }
}

This creates a new Java class called IntegerValueOf with a main method.

Taking Input From User

We will use the Scanner class to take input from the user. Add the following lines of code inside the main method:

System.out.println("Enter the value: ");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();

This code will prompt the user to enter a value and then take input from the user and store it in an integer variable called input.

Using valueOf Method

Now that we have taken input from the user, we can use the Integer.valueOf method to get the equivalent Integer object of the integer primitive value entered by the user. Add the following line of code after the input variable:

Integer integerObj = Integer.valueOf(input);

This code creates a new Integer object called integerObj by using the valueOf method with the input variable as its argument.

Displaying Result

Finally, we will display the integerObj value to the user. Add the following line of code after the previous line:

System.out.println("Equivalent Integer object Value: " + integerObj);

This code will display the equivalent Integer object value to the user.

Running the Program in the Terminal

We have completed the Java program. Save the file IntegerValueOf.java and run the following command in the terminal to compile and run the program:

javac IntegerValueOf.java && java IntegerValueOf

This command will compile the IntegerValueOf.java file and execute the program. The program will prompt the user to enter a value, and then display the equivalent Integer object value to the user.

Summary

In this lab, we learned about the Integer.valueOf(int i) method in Java. We created a Java program that takes input from the user and uses the valueOf method to get the equivalent Integer object value of the integer primitive value entered by the user. Finally, we displayed the result to the user by using the System.out.println() method. This method is useful in situations where an equivalent Integer object value is required instead of the primitive type value.

Other Java Tutorials you may like