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
  1. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    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);
        }
    }

    chevron_right

    
    

    Output:

    Integer using first variant of praseInt = 1234
    Integer using second (radix) variant of praseInt = 4660
    
  2. 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    class GfG {
        public static void main(String args[])
        {
            String str = "1234";
      
            int num1 = Integer.valueOf(str);
            System.out.println("Integer using"
                               + " valueOf() = "
                               + num1);
        }
    }

    chevron_right

    
    

    Output:

    Integer using valueOf() = 1234
    


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.