This example shows how to convert string to short in Java using the parseShort method of the Short wrapper class including out-of-range values.
How to convert string to short in Java?
To convert a string to short value, use the parseShort
method of the Short wrapper class.
1 |
public static short parseShort(String strShortNumber) |
This method returns a short primitive value by parsing the input string value.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.javacodeexamples.basic; public class StringToShortExample { public static void main(String[] args){ String strNumber = "832"; Short s = Short.parseShort(strNumber); System.out.println("String to short: " + s); } } |
Output
1 |
String to short: 832 |
Important Note:
All the characters in the string must be digits, except for the first character of the string which can be a “-“(minus sign) to indicate a negative number value.
If any of the characters in the string (except for the first character if it is a minus sign) is a non-digit character, the NumberFormatException exception is thrown.
1 2 |
String strNumber = "463.1043"; Short s = Short.parseShort(strNumber); |
Output
1 2 3 4 5 6 |
Exception in thread "main" java.lang.NumberFormatException: For input string: "463.1043" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Short.parseShort(Unknown Source) at java.lang.Short.parseShort(Unknown Source) at com.javacodeexamples.basic.StringToShortExample.main(StringToShortExample.java:9) |
Here are some of the example string values and their possible conversion to short value output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
//65 System.out.println( Short.parseShort("65") ); //NumberFormatException System.out.println( Short.parseShort("5423.21") ); //-523 System.out.println( Short.parseShort("-523") ); //NumberFormatException //System.out.println( Short.parseShort("6f45") ); //remove leading zeros, 675 System.out.println( Short.parseShort("000675") ); //NumberFormatException in older version of Java, 645 in newer versions System.out.println( Short.parseShort("+645") ); /* * Empty string is not allowed, * NumberFormatException is thrown for input String: "" */ System.out.println( Short.parseShort("") ); |
Please note that “+” (plus sign) is allowed as the first character of the string in newer versions of Java. In the older versions, if a string has a plus sign as the first character, the parseShort
method throws NumberFormatException.
Java short data type range
The short data type in Java is a 16 bit signed integer value. The range of the short variable in Java is -32768 to 32767 (both inclusive). So a short variable can have a minimum value of -32768 while the maximum value it can hold is 32767.
What happens when the string contains a number that is not in the range of the short?
When a string object contains a number that is not in the range of the short data type, and we try to convert, the parseShort
throws NumberFormatException with a message saying that value is out of range as given below.
1 |
System.out.println( Short.parseShort("-32769") ); |
Output
1 2 3 4 |
Exception in thread "main" java.lang.NumberFormatException: Value out of range. Value:"-32769" Radix:10 at java.lang.Short.parseShort(Unknown Source) at java.lang.Short.parseShort(Unknown Source) at com.javacodeexamples.basic.StringToShortExample.main(StringToShortExample.java:13) |
This example is a part of the Java Basic Examples and Java Type conversion Tutorial.
Please let me know your views in the comments section below.