Java Basic Examples

Convert String to Primitive Example

This Java example shows how to convert String object to Java primitive data type.

How to convert String to primitive value in Java?

Java wrapper classes provide parseXXX() static method which takes String as an argument and convert it to primitive value.

a) Convert String to int using Integer wrapper class

Use parseInt(String s) method of Integer wrapper class to convert from String to int value.

Note: parseInt(String s) method throws NumberFormatException if the argument String contains non digit characters except for “-“ (minus) sign to indicate negative value as a first character in the String.

b) Convert String to long using Long wrapper class

Use parseLong(String s) method of Long wrapper class to convert from String to long value.

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

c) Convert String to float using Float wrapper class

Use parseFloat(String s) method of Float wrapper class to convert from String to float value.

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

d) Convert String to double using Double wrapper class

Use parseDoule(String s) method of Double wrapper class to convert from String to double value.

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

e) Convert String to boolean using Boolean wrapper class

Use parseBoolean(String s) method of Boolean wrapper class to convert from String to boolean value.

Note: parseBoolean(String s) 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 charAt(int index) method of String class to convert from String to char value.

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

g) Convert String to byte using Byte wrapper class

Use parseByte(String s) method of Byte wrapper class to convert from String to byte value.

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

h) Convert String to short using Short wrapper class

Use parseShort(String s) method of Short wrapper class to convert from String to short value.

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

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

Want to learn quickly?
Try one of the many quizzes. I promise you will not be disappointed.

Tags