Java.lang.Short.parseShort() Method


Advertisements


Description

The java.lang.Short.parseShort(String s, int radix) method parses the string argument as a signed short in the radix specified by the second argument.

Declaration

Following is the declaration for java.lang.Short.parseShort() method

public static short parseShort(String s, int radix) throws NumberFormatException

Parameters

  • s -- This is a String containing the short representation to be parsed.

  • radix -- This is the radix to be used while parsing s

Return Value

This method returns the short represented by the string argument in the specified radix.

Exception

  • NumberFormatException -- If the string does not contain a parsable short.

Example

The following example shows the usage of java.lang.Short.parseShort() method.

package com.tutorialspoint;

import java.lang.*;

public class ShortDemo {

   public static void main(String[] args) {

   String str = "1000";

   // returns signed decimal short value of string
   short shortValue = Short.parseShort(str); 
    
   // prints signed decimalshort value
   System.out.println("Signed decimal short value for given String is =
   " + shortValue);  
	 
   // returns the string argument as a signed short in the radix
   shortValue = Short.parseShort(str,2);
   System.out.println("Signed decimal short value for specified String
   with radix 2 is = " + shortValue);

   // returns the string argument as a signed short in the radix
   shortValue = Short.parseShort("11",8);
   System.out.println("Signed decimal short value for specified String
   with radix 8 is = " + shortValue);
  }
}   

Let us compile and run the above program, this will produce the following result:

Signed decimal short value for given String is = 1000
Signed decimal short value for specified String with radix 2 is = 8
Signed decimal short value for specified String with radix 8 is = 9

java_lang_short.htm

Advertisements