Parsing Strings
All primitive data types — except char
— have methods that allow you to convert a string value to the primitive type. These methods are listed in the following table.
Wrapper |
parse Method |
Example Statement |
|
|
|
|
|
|
|
|
l |
|
|
|
|
|
|
|
|
|
|
|
|
Here’s an example that converts a String
to an int
:
String s = “10”;
int x = Integer.parseInt(s);
Note: An exception is thrown if the string does not contain a value that can be converted to a number of the appropriate type.