java.lang.Short parseShort​(String s)

Description

The Short.parseShort​(String s) java method is used primarily in parsing a String method argument into a Short object. The Short object is a wrapper class for the short primitive data type of java API. Even though the conversion of String to Short object is a basic stuff, I am suggesting that you should have a mastery on it. In my years of java programming, I have encountered so many instances that I have to convert from String object to another data type. The Short.parseShort method is a very convenient Java API to transform a user input in String format into a more robust object type which in this case the Short.

This method parses the string argument as a signed decimal short. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ‘-‘ (‘\u002D’) to indicate a negative value or an ASCII plus sign ‘+’ (‘\u002B’) to indicate a positive value. The resulting short value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseShort(java.lang.String, int) method.

Make a note that the parseShort method of Short class is static thus it should be accessed statically. Mean to say the we would be calling this method in this format:

Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java parseShort method non statically.

Important Notes:

  • This method throws NumberFormatException if the string method argument does not contain a parsable short.

Method Syntax

public static short parseShort​(String s) throws NumberFormatException

Method Argument

Data Type Parameter Description
String s a String containing the short representation to be parsed

Method Returns

The parseShort​(String s) method of Short class returns the short value represented by the argument in decimal.

Compatibility

Requires Java 1.1 and up

Java Short parseShort​(String s) Example

Below is a simple java example on the usage of parseShort​(String s) method of Short class.

Sample Output

Below is the sample output when you run the above example.

Java Short parseShort(String) method example output