java.lang.Short decode​(String nm)

Description

The decode​(String nm) method of Short class decodes a String into a Short. Accepts decimal, hexadecimal, and octal numbers given by the following grammar:

DecodableString:
Signopt DecimalNumeral
Signopt 0x HexDigits
Signopt 0X HexDigits
Signopt # HexDigits
Signopt 0 OctalDigits

Sign:

+

DecimalNumeral, HexDigits, and OctalDigits are as defined in section 3.10.1 of The Java™ Language Specification, except that underscores are not accepted between digits.
The sequence of characters following an optional sign and/or radix specifier (“0x”, “0X”, “#”, or leading zero) is parsed as by the Short.parseShort method with the indicated radix (10, 16, or 8). This sequence of characters must represent a positive value or a NumberFormatException will be thrown. The result is negated if first character of the specified String is the minus sign. No whitespace characters are permitted in the String.

Important Notes:

  • This method throws NumberFormatException, if the String does not contain a parsable short.
  • the decode​(String nm) method of Short class is static thus it should be accessed statically which means the we would be calling this method in this format: Short.decode​(String nm)

    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 decode method non statically

Method Syntax

public static Short decode​(String nm) throws NumberFormatException

Method Argument

Data Type Parameter Description
String nm the String to decode.

Method Returns

The decode​(String nm) method of Short class returns a Short object holding the short value represented by nm

Compatibility

Requires Java 1.0 and up

Java Short decode​(String nm) Example

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

The above example will ask for user input and will decode it to short value. Make sure to follow the format allowed as mentioned in the description.

Sample Output

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

Java Short decode(String nm) method example output