- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - Integer parseInt() method
Description
The Java Integer parseInt(String s, int radix) method parses the string argument s as a signed integer in the radix specified by the second argument radix. Some examples can be seen here −
parseInt("0", 10) returns 0 parseInt("222", 10) returns 222 parseInt("-0", 10) returns 0 parseInt("-BB", 16) returns -187 parseInt("1010110", 2) returns 86 parseInt("2147483647", 10) returns 2147483647 parseInt("-2147483648", 10) returns -2147483648 parseInt("2147483648", 10) throws a NumberFormatException parseInt("99", 8) throws a NumberFormatException parseInt("Kona", 10) throws a NumberFormatException parseInt("ADMIN", 27) returns 5586836
Declaration
Following is the declaration for java.lang.Integer.parseInt() method
public static int parseInt(String s, int radix) throws NumberFormatException
Parameters
s − This is a String containing the integer representation to be parsed.
radix − This is the radix to be used while parsing s.
Return Value
This method returns the integer represented by the string argument in the specified radix.
Exception
NumberFormatException − if the string does not contain a parsable int.
Example 1
The following example shows the usage of Integer parseInt() method to parse a Integer object from a string containing decimal number. We've created a String variable and assign it a string containing decimal number. Then using parseInt method, we're obtaining the Integer object and printing it.
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { String str = "50"; /* returns an Integer object holding the int value represented by string str */ System.out.println("Number = " + Integer.parseInt(str, 10)); } }
Output
Let us compile and run the above program, this will produce the following result −
Number = 50
Example 2
The following example shows the usage of Integer parseInt() method to get a Integer object from a string containing a octal number. We've created a String variable and assign it a string containing an octal number. Then using parseInt method, we're obtaining the Integer object and printing it.
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { String str = "50"; /* returns an Integer object holding the octal int value represented by string str */ System.out.println("Number = " + Integer.parseInt(str,8)); } }
Output
Let us compile and run the above program, this will produce the following result −
Number = 40
Example 3
The following example shows the usage of Integer parseInt() method to get a Integer object from a string containing a hexadecimal number. We've created a String variable and assign it a string containing an hexadecimal number. Then using parseInt method, we're obtaining an Integer object and printed.
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { String str = "50"; /* returns an Integer object holding the hexadecimal int value represented by string str */ System.out.println("Number = " + Integer.parseInt(str,16)); } }
Output
Let us compile and run the above program, this will produce the following result −
Number = 80