static Short valueOf(String s, int radix)
描述 (Description)
java.lang.Short.valueOf(String s, int radix)方法返回一个Short对象,该对象包含使用第二个参数给出的radix解析时从指定String中提取的值。
声明 (Declaration)
以下是java.lang.Short.valueOf()方法的声明
public static Short valueOf(String s, int radix) throws NumberFormatException
参数 (Parameters)
s - 这是要解析的字符串。
radix - 这是用于解释s的基数
返回值 (Return Value)
此方法返回一个Short对象,该对象包含指定基数中字符串参数表示的值。
异常 (Exception)
NumberFormatException - 如果String不包含可解析的short。
例子 (Example)
以下示例显示了java.lang.Short.valueOf()方法的用法。
package com.iowiki;
import java.lang.*;
public class ShortDemo {
public static void main(String[] args) {
short shortNum = 100;
String str = "1000";
// returns Short object representing given short value
Short ShortValue = Short.valueOf(shortNum);
// displays the short object value
System.out.println("Short object representing the specified short value =
" + ShortValue);
// returns a Short object holding the value given by the specified String
ShortValue = Short.valueOf(str);
// displays the short object value
System.out.println("Short object holding the value given by the specified
String = " + ShortValue);
/* returns a Short object holding the value from the specified String
according to radix. */
ShortValue = Short.valueOf(str , 2) ;
// displays the short object value
System.out.println("Short object value for specified String with radix 2
=" + ShortValue);
// returns a Short object holding the value for string to radix
ShortValue = Short.valueOf("15" , 8) ;
// displays the short object value
System.out.println("Short object value String 15 with radix 8
= " + ShortValue);
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
Short object representing the specified short value = 100
Short object holding the value given by the specified String = 1000
Short object value for specified String with radix 2 = 8
Short object value String 15 with radix 8 = 13