Skip to content

Convert String to Primitive Example

This Java example shows how to convert a String object to primitive data types in Java using parseXXX methods of the respective wrapper classes.

How to convert String to primitive value in Java?

Java wrapper classes provide the parseXXX static methods that take a string as an argument and convert it to respective primitive values.

a) Convert String to int using the Integer wrapper class

Use the parseInt method of the Integer wrapper class to convert from String to int value.

Note: The parseInt method throws a NumberFormatException exception if the argument string contains non-digit characters except for the “-” (minus) sign to indicate negative numeric value as the first character of the string.

b) Convert String to long using the Long wrapper class

Use the parseLong method of the Long wrapper class to convert from string to long value.

Note: The parseLong method throws NumberFormatException exception if the argument string contains non-digit characters except for the “-” (minus) sign to indicate negative numeric value as the first character of the String. Even the characters “L” or “l” are not permitted as the last character to indicate the long type.

c) Convert String to float using the Float wrapper class

Use the parseFloat method of the Float wrapper class to convert from string to float value.

Note: The parseFloat method throws a NumberFormatException exception if the argument string cannot be converted to a float value. If the string contains any non-digit characters except for the “-” (minus) sign (as a first character of the string) or “.” (decimal point), NumberFormatException will be thrown.

d) Convert String to double using the Double wrapper class

Use the parseDoule method of the Double wrapper class to convert from string to a double value.

Note: The parseDouble method throws a NumberFormatException exception if the argument string cannot be converted to a double. If the string contains any non-digit characters except for the “-” (minus) sign (as a first character of the string) or “.” (a decimal point), NumberFormatException will be thrown.

e) Convert String to boolean using the Boolean wrapper class

Use the parseBoolean method of the Boolean wrapper class to convert from string to a boolean value.

Note: The parseBoolean method returns true if the argument string is not null and is equal to “true” ignoring case. For every other string value, it returns false. Some of the example string values are given below.

f) Convert String to char

Use the charAt method of the String class to convert from string to a char value.

Since the ASCII char represents the number internally, you can also use the following code to convert string to a char.

g) Convert String to byte using the Byte wrapper class

Use the parseByte method of the Byte wrapper class to convert from string to a byte value.

Note: The parseByte method throws a NumberFormatException if the argument string contains non-digit characters except for the “-” (minus) sign to indicate negative numeric value as the first character of the String.

h) Convert String to short using the Short wrapper class

Use the parseShort method of the Short wrapper class to convert from string to a short value.

Note: The parseShort method throws a NumberFormatException if the argument string contains non-digit characters except for the “-” (minus) sign to indicate negative numeric value as the first character of the String.

This example is a part of the Java Basic Examples and String in Java tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.