java.lang.Integer valueOf(String s)

Description :

This java tutorial shows how to use the valueOf(String s) method of Integer class under java.lang package. This method return a Integer object type which is the parsed equivalent of the String method argument

Method Syntax :

public static Integer valueOf(String s) throws NumberFormatException

Parameter Input :

DataType Parameter Description
String s the String value that we want to be converted into Integer object

Method Returns :

The toOctalString(int i) method simply returns the octal string equivalent of int method parameter.

Compatibility Version :

Requires Java 1.0 and up

Exception :

NumberFormatException

– The NumberFormatException will be thrown if the we have breached the Minimum and Maximum value of the String input. And also one possible case is if the input is not parsable int.

Discussion :

The valueOf(String s) method is static thus we should invoke it statically for example Integer.valueOf(String s). This method simply converts the String input into Integer object. This method yields the same result as Integer.parseInt(String) but the only difference is that the valueOf(String s) method returns the wrapper class Integer instead of the primitive int. And also the valueOf(String s) is equal to valueOf(String s, int radix). This method is assumed to parse the String input of base 10.

There is maximum and minimum allowable value to be converted and this is can be determined using the two static method Integer.MAX_VALUE and Integer.MIN_VALUE respectively. If this is breached, the InputMismatchException will be thrown.

Java Code Example :

This java example source code demonstrates the use of valueOf(String s) method of Integer class. Basically we have used Scanner object to get the user input from the console and transform the same into 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(String sx) method of Integer class.
 * Convert string input into Integer object
 */

public class IntegerValueOfSting {

	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
		String strValue = scan.nextLine();		
		// close the scanner object
		scan.close();
		// print the value in decimal format
		out.println("Integer Value:" + Integer.valueOf(strValue));

	}

}

Sample Output :

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

Enter Desired Value:123
Integer Value:123

Exception Scenario :

Enter Desired Value:12131231231231231231231231231231
Exception in thread "main" java.lang.NumberFormatException: For input string: "12131231231231231231231231231231"
	at java.lang.NumberFormatException.forInputString(Unknown Source)
	at java.lang.Integer.parseInt(Unknown Source)
	at java.lang.Integer.valueOf(Unknown Source)
	at com.javatutorialhq.java.examples.IntegerValueOfSting.main(IntegerValueOfSting.java:25)

Similar Method :

  • N/A

Suggested Reading List :

References :