

throws NumberFormatException - value too large Al the following formats are supported by this method: Signopt DecimalNumeralĪs with Integer.valueOf(), this method returns an Integer object rather than a plain int. An optional “+” or “-” sign can precede the number. code()įor parsing an integer starting with these prefixes: “0” for octal, “0x”, “0X” and “#” for hex, you can use the method code(String). Use this method when you need an Integer object rather than a bare int. This method invokes Integer.parseInt(String) and creates an Integer from the result. The static method Integer.valueOf() works similar to Integer.parseInt() with the difference that the method returns an Integer object rather than an int value. Int value = Integer.parseInt("123aghi", 20) returns 70966758 - "h" and "i" are valid characters for radix 20. throws NumberFormatException - contains character "9" not valid for radix 8 To explicitly specify the radix, use Integer.parseInt(String,int) and pass the radix as the second argument. throws NumberFormatException - too large throws NumberFormatException - contains text throws NumberFormatException - contains "."

Additionally a string containing a number larger than Integer.MAX_VALUE ( 2 31 - 1) also result in a NumberFormatException. Illegal characters within the string (including period “. Int value = Integer.parseInt("-43") // return -43 int value = Integer.parseInt("25") // returns 25 The string can contain “ +” and “ -” characters at the start to indicate a positive or negative number. The string value is parsed assuming a radix of 10. Integer.parseInt(String) can parse a string and return a plain int value. We present a few of them here with a discussion of the pros and cons of each. There are several ways of converting a String to an integer in Java.
