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.

package com.javatutorialhq.java.examples;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of  
 * decode(String nm) method of Short class
 */

public class ShortDecodeExample {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("Enter a value:");

		// declare a scanner object to read the user input
		Scanner s = new Scanner(System.in);

		// assign the input to a variable
		String value = s.nextLine();

		// get the decode() method result
		Short result = Short.decode(value);

		// print the result
		System.out.println("Result:" + result);

		// close the scanner object
		s.close();

		/*
		 * Format available 
		 * 0x HexDigits 
		 * 0X HexDigits 
		 * # HexDigits 
		 * 0 OctalDigits
		 */
	}

}

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