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.

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