Input: str = "1234" Output: 1234 Input: str = "456" Output: 456
- Convert using Integer.parseInt(String)
The Integer class has a static method that returns an integer object representing the specified String parameter.
public static int parseInt(String str) throws NumberFormatException or public static int parseInt(String str, int radix) throws NumberFormatException
- str: A string which needs to be converted to the integer. It can also have the first character as a minus sign ‘-‘ (‘\u002D’) or plus sign ‘+’ (‘\u002B’) to represent the sign of the number.
- radix: The radix used while the string is being parsed. This parameter is only specific to the second variant of the method.
Exceptions: NumberFormatException is thrown by this method if any of the following situations occurs:
For both the variants:
- String is null or of zero length
- The value represented by the string is not a value of type int
- Specifically for the parseInt(String s, int radix) variant of the function:
- The second argument radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX
- Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign ‘-‘ (‘\u002D’) or plus sign ‘+’ (‘\u002B’) provided that the string is longer than length 1
String str =
num1 = Integer.parseInt(str);
"Integer using "
"first variant of"
" praseInt = "
num2 = Integer.parseInt(str,
"Integer using "
"second (radix) variant"
" of praseInt = "
Integer using first variant of praseInt = 1234 Integer using second (radix) variant of praseInt = 4660
- Convert using Integer.valueOf(String)
public static Integer valueOf(String str)
Parameters: This method accepts single parameter str of String type that is to be parsed.
Return Value: The method returns an Integer object holding the value represented by the string argument.
String str =
num1 = Integer.valueOf(str);
" valueOf() = "
Integer using valueOf() = 1234
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Different ways for Integer to String Conversions In Java
- Sort a String in Java (2 different ways)
- Different Ways to Print First K Characters of the String in Java
- Different Ways to Remove all the Digits from String in Java
- Different Ways to Generate String by using Characters and Numbers in Java
- Program to Convert Set of Integer to Array of Integer in Java
- Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples
- Different ways of Method Overloading in Java
- Different ways of Reading a text file in Java
- 3 Different ways to print Exception messages in Java
- Different ways to create objects in Java
- Integer.valueOf() vs Integer.parseInt() with Examples
- Different ways to hide Action bar in Android with Examples
- Print all possible ways to convert one string into another string | Edit-Distance
- String to Integer in Java - parseInt()
- Check if a given string is a valid number (Integer or Floating Point) in Java
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Program to convert List of Integer to List of String in Java
- Program to Convert Set of Integer to Set of String in Java
- Program to convert set of String to set of Integer in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.