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.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access