Java Short parseShort()方法

java.lang.Short.parseShort(String s, int radix) 方法解析字符串参数作为有符号short 在由第二个参数指定的基数。

1 语法

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

2 参数

s : 这是要分析包含short表示的字符串。

radix: 这是用来在解析s的基数

3 返回值

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

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * Java Short parseShort()方法
 */
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

 

热门文章

优秀文章