Java.lang.Double 类

java.lang.Double.shortValue() 方法在缩小基元转换后返回此 Double 的值作为 Short .

语法

public short shortValue()

参数

无需参数。

返回值

返回转换为short类型的该对象表示的双精度值。

异常

无。

示例:

In在下面的示例中,java.lang.Double.shortValue()方法在转换为short类型后返回一个Double对象。

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //创建Double对象
    Double val1 = 25.2;
    Double val2 = -25.2;

    //打印Double对象的shortValue
    System.out.println("shortValue of the val1 is: " + val1.shortValue()); 
    System.out.println("shortValue of the val2 is: " + val2.shortValue());   
  }
}

上面的输出代码为:

shortValue of the val1 is: 25
shortValue of the val2 is: -25