Java - Byte shortValue() 方法



描述

Java Byte shortValue() 方法返回此 Byte 作為 short 型別的值。

宣告

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

public short shortValue()

指定於

Number 類中的 shortValue

引數

返回值

此方法在轉換為 short 型別後返回此物件表示的數值。

異常

使用 new 運算子建立的 Byte 物件獲取 Short 值示例

以下示例演示了使用 new 運算子建立的 Byte 物件的 Byte shortValue() 方法的使用。我們建立了一個 Byte 變數,併為其分配了一個使用 new 運算子建立的 Byte 物件。然後建立一個 short 變數,並使用 shortValue() 方法為其分配一個 short 值,然後列印結果。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte object b
      Byte b;

      // assign value to b
      b = new Byte("100");

      // create a short primitive d
      short d;

      // assign primitive value of b to d
      d = b.shortValue();

      String str = "Primitive short value of Byte object " + b + " is " + d;

      // print d value
      System.out.println( str );
   }
}

輸出

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

Primitive short value of Byte object 100 is 100

使用 valueOf(String) 方法建立的 Byte 物件獲取 Short 值示例

以下示例演示了使用 valueOf(String) 方法建立的 Byte 物件的 Byte shortValue() 方法的使用。我們建立了一個 Byte 變數,併為其分配了一個使用 valueOf(String) 方法建立的 Byte 物件。然後建立一個 short 變數,並使用 shortValue() 方法為其分配一個 short 值,然後列印結果。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte object b
      Byte b;

      // assign value to b
      b = Byte.valueOf("100");

      // create a short primitive d
      short d;

      // assign primitive value of b to d
      d = b.shortValue();

      String str = "Primitive short value of Byte object " + b + " is " + d;

      // print d value
      System.out.println( str );
   }
}

輸出

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

Primitive short value of Byte object 100 is 100

使用 valueOf(byte) 方法建立的 Byte 物件獲取 Short 值示例

以下示例演示了使用 valueOf(byte) 方法建立的 Byte 物件的 Byte shortValue() 方法的使用。我們建立了一個 Byte 變數,併為其分配了一個使用 valueOf(byte) 方法建立的 Byte 物件。然後建立一個 short 變數,並使用 shortValue() 方法為其分配一個 short 值,然後列印結果。

package com.tutorialspoint;
public class ByteDemo {
   public static void main(String[] args) {

      // create a Byte object b
      Byte b;

      // assign value to b
      b = Byte.valueOf((byte) 100);

      // create a short primitive d
      short d;

      // assign primitive value of b to d
      d = b.shortValue();

      String str = "Primitive byte value of Byte object " + b + " is " + d;

      // print d value
      System.out.println( str );
   }
}

輸出

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

Primitive byte value of Byte object 100 is 100
java_lang_byte.htm
廣告