当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.lang.Short.parseShort()用法及代码示例



描述

这个java.lang.Short.parseShort(String s, int radix) 方法将字符串参数解析为radix由第二个参数指定。

声明

以下是声明java.lang.Short.parseShort()方法

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

参数

  • s- 这是一个包含要解析的短表示的字符串。

  • radix- 这是解析 s 时要使用的基数

返回值

此方法返回由指定基数中的字符串参数表示的 short。

异常

NumberFormatException- 如果字符串不包含可解析的 short。

示例

下面的例子展示了 java.lang.Short.parseShort() 方法的用法。

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);
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

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.parseShort() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。