Java - Double shortValue() 方法



描述

Java Double shortValue() 方法返回此 Double 物件的 short 值。

宣告

以下是 java.lang.Double.shortValue() 方法的宣告

public short shortValue()

引數

返回值

此方法返回此物件表示的 short 值。

異常

從 Double 物件獲取 short 值示例

以下示例演示瞭如何使用 Double shortValue() 方法獲取給定正 Double 物件的 short 原語值。我們已使用給定的正 short 值初始化了一個 Double 物件。然後使用 shortValue() 方法,我們列印其值。

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {
      Double d = new Double("15.30");
   
      //return the short value
      System.out.println("Value = " + d.shortValue());
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = 15

使用負值的 Double 物件獲取 short 值示例

以下示例演示瞭如何使用 Double shortValue() 方法獲取給定負 Double 物件的 short 原語值。我們已使用給定的正 short 值初始化了一個 Double 物件。然後使用 shortValue() 方法,我們列印其值。

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {
      Double d = new Double("-15.30");
   
      //return the short value
      System.out.println("Value = " + d.shortValue());
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = -15

使用負零值的 Double 物件獲取 short 值示例

以下示例演示瞭如何使用 Double shortValue() 方法獲取給定值為零的負 Double 物件的 short 原語值。我們已使用給定的正 short 值初始化了一個 Double 物件。然後使用 shortValue() 方法,我們列印其值。

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {
      Double d = new Double("-0.0");
   
      //return the short value
      System.out.println("Value = " + d.shortValue());
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = 0

使用正零值的 Double 物件獲取 short 值示例

以下示例演示瞭如何使用 Double shortValue() 方法獲取給定值為零的正 Double 物件的 short 原語值。我們已使用給定的正 short 值初始化了一個 Double 物件。然後使用 shortValue() 方法,我們列印其值。

package com.tutorialspoint;
public class DoubleDemo {
   public static void main(String[] args) {
      Double d = new Double("+0.0");
   
      //return the short value
      System.out.println("Value = " + d.shortValue()); 
   }
} 

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Value = 0
java_lang_double.htm
廣告