Different ways for String to Integer Conversions in Java Last Updated : 29 May, 2019 Comments Improve Suggest changes Like Article Like Report 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: JAVA class GfG { public static void main(String args[]) { String str = "1234"; int num1 = Integer.parseInt(str); System.out.println("Integer using " + "first variant of" + " praseInt = " + num1); int num2 = Integer.parseInt(str, 16); System.out.println("Integer using " + "second (radix) variant" + " of praseInt = " + num2); } } Output: 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: JAVA class GfG { public static void main(String args[]) { String str = "1234"; int num1 = Integer.valueOf(str); System.out.println("Integer using" + " valueOf() = " + num1); } } Output: Integer using valueOf() = 1234 Comment More infoAdvertise with us Next Article Different ways for String to Integer Conversions in Java C code_r Follow Improve Article Tags : Java Java-Strings Practice Tags : JavaJava-Strings Similar Reads Java Convert int to String - Different Ways of Conversion Converting an int to a String is an important type conversion. Many operations can be performed over a string, while we are limited when it comes to integers. We have a wide varied list of in-built methods in the String class that help us perform hassle-free operations. Suppose we are required to co 9 min read 5 Ways to Convert Double to Integer in Java Double is a primitive data type in Java that is used to represent double-precision floating point numbers. It uses the IEEE 754 standard for representing floating point numbers and has a default value of 0.0d. The double data type is the default data type for decimal numbers in Java and has a defaul 5 min read How to Convert a String to an int in Java? In Java, converting a String to an int is done using methods from the Integer class. The methods used for this conversion are Integer.parseInt() and Integer.valueOf(). Example:The Integer.parseInt() is a commonly used method to convert a string to an integer in Java. This method converts a numeric s 1 min read Convert String to Double in Java In this article, we will convert a String to a Double in Java. There are three methods for this conversion from String to Double, as mentioned below in the article.Methods for String-to-Double ConversionDifferent ways for converting a String to a Double are mentioned below:Using the parseDouble() me 3 min read Different Ways to Generate String by using Characters and Numbers in Java Given a number num and String str, the task is to generate the new String by extracting the character from the string by using the index value of numbers. Examples: Input: str = âGeeksforGeeksâ num = 858 Output: GfG Explanation: The 8th, 5th, and 8th position of the characters are extracting from th 3 min read Like