Integer.valueOf() method in Java with Examples

valueOf() is a static method of the Integer wrapper class in Java that returns an Integer object corresponding to the argument(s) passed in it. In this post, we will look at the valueOf() method in detail. The valueOf() method has three overloaded methods –

Let’s look at the overloaded methods one by one.

public static Integer valueOf(int i)

  • What does it do? It will take an int primitive into the arguments and return an Integer instance representing the specified int value. This is also one of the preferred ways to make an Integer instance, as this method yields better space and time performance than using the constructor
  • What does it return? It will return an Integer instance representing the int value passed in the arguments
Code Example
public class Codekru {

	public static void main(String[] args) {

		int value = 200;
		Integer i = Integer.valueOf(value);

		System.out.println("Integer instance value: " + i);

	}
}

Output –

Integer instance value: 200

public static Integer valueOf(String s)

  • What does it do? It will take a string as an argument and return an Integer instance holding the value of the specified string. The string should only contain the decimal characters and optionally a positive or negative sign at the start. If there are any other characters, then the function will throw a NumberFormatException
  • What does it return? It will return an Integer instance holding the value of the specified string
String s1 = "20"; // this is correct, as string only contains decimal characters

String s2 = "-20";    // this is also correct, as minus sign
                      //can be put as the first character of the string

String s3 = "+20";    // this is also correct

String s3 = "twenty20"  // this is wrong as string contains non-decimal characters
Code Example
public class Codekru {

	public static void main(String[] args) {

		String str1 = "20";
		String str2 = "+20";
		String str3 = "-20";

		Integer i1 = Integer.valueOf(str1);
		Integer i2 = Integer.valueOf(str2);
		Integer i3 = Integer.valueOf(str3);

		System.out.println("Integer value representing str1: " + i1);
		System.out.println("Integer value representing str2: " + i2);
		System.out.println("Integer value representing str3: " + i3);
	}
}

Output –

Integer value representing str1: 20
Integer value representing str2: 20
Integer value representing str3: -20
Internal implementation of the valueOf(String s)

valueOf(String s) internally uses the parseInt(String s, int radix) to convert the string into an int primitive value and then convert the int primitive value to an Integer instance by using the valueOf(int i) method.

    public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }

public static Integer valueOf(String s, int radix)

  • What does it do? After parsing with the radix passed as the second argument, it will return an Integer instance holding the value represented by the string. Here, the string can contain other than decimal characters provided that the radix supports it. Like, we can have A, B, C, D, E, F, and decimal characters with the radix 16.
    The radix value should be between 2 and 36
  • What does it return? It will return an Integer instance

Below is the sample representation of converting a number with any radix. We have used radix as in our example.

Conversion of "220" string with radix 4
Code Example
public class Codekru {

	public static void main(String[] args) {

		String str1 = "220";
		String str2 = "+20A";
		String str3 = "-20";

		Integer i1 = Integer.valueOf(str1, 4);
		Integer i2 = Integer.valueOf(str2, 16);
		Integer i3 = Integer.valueOf(str3, 5);

		System.out.println("Integer value representing str1: " + i1);
		System.out.println("Integer value representing str2: " + i2);
		System.out.println("Integer value representing str3: " + i3);
	}
}

Output –

Integer value representing str1: 40
Integer value representing str2: 522
Integer value representing str3: -10
Internal implementation of the valueOf(String s, int radix)

valueOf(String s, int radix) internally uses the parseInt(String s, int radix) to convert the string into an int primitive value and then convert the int primitive value to an Integer instance by using the valueOf(int i) method.

    public static Integer valueOf(String s, int radix) throws NumberFormatException {
        return Integer.valueOf(parseInt(s,radix));
    }

The What If scenarios

Q – What if we pass alphabets in valueOf(String s)?

It will throw a NumberFormatException as illustrated by the below program.

public class Codekru {

	public static void main(String[] args) {

		String str = "220A";

		Integer i1 = Integer.valueOf(str);

		System.out.println("Integer value representing str: " + i1);
	}
}

Output –

Exception iException in thread "main" java.lang.NumberFormatException: For input string: "220A"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
	at java.base/java.lang.Integer.parseInt(Integer.java:652)
	at java.base/java.lang.Integer.valueOf(Integer.java:983)
Q – What if we pass a null string in valueOf(String s)?

It will throw a NumberFormatException here as well.

public class Codekru {

	public static void main(String[] args) {

		String str = null;

		Integer i1 = Integer.valueOf(str);

		System.out.println("Integer value representing str: " + i1);
	}
}

Output –

Exception in thread "main" java.lang.NumberFormatException: null
	at java.base/java.lang.Integer.parseInt(Integer.java:614)
	at java.base/java.lang.Integer.valueOf(Integer.java:983)
Q – What if we pass an empty string in valueOf(String s)?
public class Codekru {

	public static void main(String[] args) {

		String str = "";

		Integer i1 = Integer.valueOf(str);

		System.out.println("Integer value representing str: " + i1);
	}
}

Output –

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
	at java.base/java.lang.Integer.parseInt(Integer.java:662)
	at java.base/java.lang.Integer.valueOf(Integer.java:983)
Q – What if we pass radix > 36 in valueOf(String s, int radix)?

Here, it will again throw a NumberFormatException.

public class Codekru {

	public static void main(String[] args) {

		String str = "1234";

		Integer i1 = Integer.valueOf(str,37);

		System.out.println("Integer value representing str: " + i1);
	}
}

Output –

Exception in thread "main" java.lang.NumberFormatException: radix 37 greater than Character.MAX_RADIX
	at java.base/java.lang.Integer.parseInt(Integer.java:623)
	at java.base/java.lang.Integer.valueOf(Integer.java:957)
Q – What if we pass radix < 2 in valueOf(String s, int radix)?
public class Codekru {

	public static void main(String[] args) {

		String str = "1234";

		Integer i1 = Integer.valueOf(str,1);

		System.out.println("Integer value representing str: " + i1);
	}
}

Output –

Exception in thread "main" java.lang.NumberFormatException: radix 1 less than Character.MIN_RADIX
	at java.base/java.lang.Integer.parseInt(Integer.java:618)
	at java.base/java.lang.Integer.valueOf(Integer.java:957)

Please visit this link to learn more about the Integer wrapper class of java and its other functions or methods.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected].

Related Articles

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *