Java.lang.Number.shortValue() 方法
描述
java.lang.Number.shortValue() 方法将指定数字的值作为短整数返回。 这可能涉及舍入或截断。
声明
以下是 java.lang.Number.shortValue() 方法的声明。
public abstract short shortValue()
参数
NA
返回值
该方法返回该对象转换为short类型后所表示的数值。
异常
NA
示例
下面的例子展示了 lang.Number.shortValue() 方法的使用。
package com.tutorialspoint;
public class NumberDemo {
public static void main(String[] args) {
// get a number as float
Float x = new Float(123456f);
// get a number as double
Double y = new Double(9876);
// print their value as short
System.out.println("x as float :" + x
+ ", x as short:" + x.shortValue());
System.out.println("y as double:" + y
+ ", y as short:" + y.shortValue());
}
}
让我们编译并运行上面的程序,这将产生下面的结果 −
x as float :123456.0, x as short:-7616 y as double:9876.0, y as short:9876

