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.
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.
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
.
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;
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.
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
.
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.
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.
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.
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.