java.lang.Integer valueOf(int i)

Description :

This java tutorial shows how to use the valueOf(int i) method of Integer class under java.lang package. This method returns the wrapper object Integer of the primitive int specified as method argument.

Method Syntax :

public static Integer valueOf(int i)

Parameter Input :

DataType Parameter Description
int i the int value that we want to convert into Integer object

Method Returns :

The valueOf(int i) method simply returns the Integer object wrapping the int parameter.

Compatibility Version :

Requires Java 1.5 and up

Exception :

N/A

Discussion :

The valueOf(int i) method is static thus we should invoke it statically for example Integer.toOctalString(int i). This method simply converts the int parameter into Integer object type. This is helpful in wrapping the int primitive data type. Sometimes its necessary to wrap the int primitive to have access to more advanced java API.

Java Code Example :

This java example source code demonstrates the use of valueOf(int i) method of Integer class. Basically we just ask for a user input and print the value as Integer object.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

import static java.lang.System.*;

/*
 * This example source code demonstrates the use of 
 * valueOf(int i) method of Integer class.
 * Convert int input into Integer object
 */

public class fIntegerValueOfInt {

	public static void main(String[] args) {
		// Ask user input
		System.out.print("Enter Desired Value:");
		// declare the scanner object
		Scanner scan = new Scanner(System.in);
		// use scanner to get the value from user console
		int intValue = scan.nextInt();		
		// close the scanner object
		scan.close();
		// print the value of Integer object
		out.println("Integer Value:" + Integer.valueOf(intValue));

	}

}

Sample Output :

Running the toOctalString() method example source code of Integer class will give you the following output

Enter Desired Value:12
Integer Value:12

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :