Different ways for String to Integer Conversions in Java
Given a String in Java, the task is to convert this String into Integer.
Examples:
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.
Syntax :
public static int parseInt(String str) throws NumberFormatException or public static int parseInt(String str, int radix) throws NumberFormatException
Parameters:
- 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
Example:
classGfG {publicstaticvoidmain(String args[]){String str ="1234";intnum1 = Integer.parseInt(str);System.out.println("Integer using "+"first variant of"+" praseInt = "+ num1);intnum2 = Integer.parseInt(str,16);System.out.println("Integer using "+"second (radix) variant"+" of praseInt = "+ num2);}}chevron_rightfilter_noneOutput:Integer using first variant of praseInt = 1234 Integer using second (radix) variant of praseInt = 4660
- Convert using Integer.valueOf(String)
Syntax:
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.
Example:
classGfG {publicstaticvoidmain(String args[]){String str ="1234";intnum1 = Integer.valueOf(str);System.out.println("Integer using"+" valueOf() = "+ num1);}}chevron_rightfilter_noneOutput:Integer using valueOf() = 1234
Recommended Posts:
- Different ways for Integer to String Conversions In Java
- String to Integer in Java - parseInt()
- Program to convert set of String to set of Integer in Java
- Program to Convert Set of Integer to Set of String in Java
- Sort a String in Java (2 different ways)
- 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 Set of Integer to Array of Integer in Java
- Program to convert List of Integer to List of String in Java
- Program to convert List of String to List of Integer in Java
- Print all possible ways to convert one string into another string | Edit-Distance
- Java.lang.Integer class in Java
- Integer toString() in Java
- Integer sum() Method in Java
- How to Read and Print an Integer value 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 contribute@geeksforgeeks.org. 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.


