Open In App

Different ways for String to Integer Conversions in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
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
  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:




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




    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
    


Last Updated : 29 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads