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.
1 2 3 4 5 6 7 8 9 10 |
String strObject = "124"; try{ int i = Integer.parseInt(strObject); System.out.println("String to int: " + i); }catch(NumberFormatException nfe){ System.out.println("Invalid number"); } |
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.
1 2 3 4 5 6 7 8 9 10 |
String strObject = "3212343"; try{ long l = Long.parseLong(strObject); System.out.println("String to long: " + l); }catch(NumberFormatException nfe){ System.out.println("Invalid number"); } |
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.
1 2 3 4 5 6 7 8 9 10 |
String strObject = "2342.432"; try{ float f = Float.parseFloat(strObject); System.out.println("String to float: " + f); }catch(NumberFormatException nfe){ System.out.println("Invalid number"); } |
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.
1 2 3 4 5 6 7 8 9 10 |
String strObject = "134232.324221"; try{ double d = Double.parseDouble(strObject); System.out.println("String to double: " + d); }catch(NumberFormatException nfe){ System.out.println("Invalid number"); } |
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.
1 2 3 4 |
String strObject = "true"; boolean b = Boolean.parseBoolean(strObject); System.out.println("String to boolean: " + b); |
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.
1 2 3 4 5 6 7 |
System.out.println( Boolean.parseBoolean("TRUE") ); //true System.out.println( Boolean.parseBoolean("True") ); //true System.out.println( Boolean.parseBoolean("true") ); //true System.out.println( Boolean.parseBoolean("123") ); //false System.out.println( Boolean.parseBoolean(null) ); //false System.out.println( Boolean.parseBoolean("") ); //false System.out.println( Boolean.parseBoolean("432.123") ); //false |
f) Convert String to char
Use the charAt
method of the String class to convert from string to a char value.
1 2 |
String strObject = "a"; char c = strObject.charAt(0); |
Since the ASCII char represents the number internally, you can also use the following code to convert string to a char.
1 2 3 4 |
String strObject = "65"; char c = (char) Integer.parseInt(strObject); System.out.println(c); |
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.
1 2 3 4 5 6 7 8 |
try{ byte b = Byte.parseByte(strObject); System.out.println("String to byte: " + b); }catch(NumberFormatException nfe){ System.out.println("Invalid number"); } |
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.
1 2 3 4 5 6 7 8 9 10 |
String strObject = "65"; try{ Short s = Short.parseShort(strObject); System.out.println("String to short: " + s); }catch(NumberFormatException nfe){ System.out.println("Invalid number"); } |
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.