Home » Java Integer valueOf() method with Examples

Java Integer valueOf() method with Examples

by Online Tutorials Library

Java Integer valueOf() Method

The valueOf() method is a static method which returns the relevant Integer Object holding the value of the argument passed. The argument can be a primitive data type, String, etc. There are three different types of Java valueOf() method which can be differentiated depending on its parameter.

These are:

  1. Java Integer valueOf(int i) Method
  2. Java Integer valueOf(String s) Method
  3. Java Integer valueOf(String s, int radix) Method

1. Java Integer valueOf(int i) Method

The valueOf(int i) method of Java Integer class returns an Integer instance representing the specified int value. This method will always accept values in the range -128 to 127 and may cache other values outside of this range.

2. Java Integer valueOf(String s) Method

The valueOf(String s) is an inbuilt method of Java which is used to returns an Integer object holding the value of the specified string. The argument is interpreted as a signed decimal integer. In other words, this method returns an Integer object equal to the value of:

3. Java Integer valueOf(String s, int radix) Method

The valueOf(String s, int radix) method is used to return an Integer object holding the value extracted from the specified string when parsed with the radix given by the second argument. In other words, this method returns an Integer object equal to the value of:

Syntax:

Following are the declaration of valueOf() method:

Parameter:

DataType Parameter Description Required/Optional
int i It is an int value specified by the user and used in converting the Integer object. Required
String s It is a type of String which will be parsed into an integer object. Required
int radix This is of integer type and used in converting the string object. Required

Returns:

Method Returns
valueOf(int i) Returns an Integer instance holding the value of the specified parameter int i.
valueOf(String s) Returns an Integer instance holding the value represented by the string argument.
valueOf(String s, int radix) Returns an Integer instance holding the value represented by the string argument in the specified radix.

Exceptions:

NumberFormatException: It throws exception when the input String with respect to specified radix is not a parsable int.

Compatibility Version:

Java 1.5 and above

Example 1

Test it Now

Output:

Value = 2  Value = -5  

Example 2

Test it Now

Output:

Output Value = 355  Output Value = -355  

Example 3

Test it Now

Output:

Desired Value is: 234  Base Number is: 8  Integer Value: 156  

Example 4

Test it Now

Output:

Enter Desired Value: CDEF  Enter Base Number: 16  Output Value: 52719  

Example 5

Test it Now

Output:

Enter Desired Value: ABCDEF  Exception in thread "main" java.lang.NumberFormatException: For input string: "ABCDEF"  at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)  at java.base/java.lang.Integer.parseInt(Integer.java:652)  at java.base/java.lang.Integer.valueOf(Integer.java:983)  at myPackage.IntegerValueOfExample5.main(IntegerValueOfExample5.java:13)  

Next Topic#

You may also like