- 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 - parseInt() Method
Description
This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two. For example parseInt() is for Integer object and parseDouble() is for Double object.
Syntax
Following are all the variants of this method −
static int parseInt(String s) static int parseInt(String s, int radix)
Parameters
Here is the detail of parameters −
s − This is a string representation of decimal.
radix − This would be used to convert String s into integer.
Return Value
parseInt(String s) − This returns an integer (decimal only).
parseInt(Strings, int i) − This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.
Example 1
In this example, we're showing the usage of parseInt() method to get int from a String. As first, we've parsed an int from a string then we parsed it using a given radix. Both are printed thereafter.
public class Test { public static void main(String args[]) { int x =Integer.parseInt("9"); int b = Integer.parseInt("444",16); System.out.println(x); System.out.println(b); } }
This will produce the following result −
Output
9 1092
Example 2
In this example, we're showing the usage of parseDouble() method to get a double from a String. We've parsed a double from a string and printed it.
public class Test { public static void main(String args[]) { double x = Double.parseDouble("9.0"); System.out.println(x); } }
This will produce the following result −
Output
9.0
Example 3
In this example, we're showing the usage of parseLong() method to get a long from a String. We've parsed a long from a string and printed it.
public class Test { public static void main(String args[]) { long x = Long.parseLong("9"); System.out.println(x); } }
This will produce the following result −
Output
9
To Continue Learning Please Login
Login with Google