Java.lang.Short.valueOf() 方法
描述
java.lang.Short.valueOf(String s) 方法返回一个包含指定 String 给定值的 Short 对象。
声明
以下是 java.lang.Short.valueOf() 方法的声明。
public static Short valueOf(String s) throws NumberFormatException
参数
s − 这是要解析的字符串。
返回值
此方法返回一个 Short 对象,其中包含字符串参数表示的值。
异常
NumberFormatException − 如果字符串不包含可解析的短。
示例
下面的例子展示了 java.lang.Short.valueOf() 方法的使用。
package com.tutorialspoint;
import java.lang.*;
public class ShortDemo {
public static void main(String[] args) {
short shortNum = 100;
String str = "600";
// 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);
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
Short object representing the specified short value = 100 Short object holding the value given by the specified String = 600

