java.lang.Short.parseShort(String s, int radix) 方法解析字串引數作為有符號short 在由第二個引數指定的基數。
以下是java.lang.Short.parseShort()方法的宣告
public static short parseShort(String s, int radix) throws NumberFormatException
s -- 這是要分析包含short表示的字串。
radix -- 這是用來在解析s的基數
此方法返回由指定基數的字串引數表示的short值。
NumberFormatException -- 如果該字串不包含一個可分析的short。
下面的例子顯示java.lang.Short.parseShort()方法的使用。
package com.yiibai; 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