java.lang.Byte.shortValue()方法範例


 java.lang.Byte.shortValue() 返回此位元組為一個short值。

宣告

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

public short shortValue()

Overrides

shortValue 在Number 類 

引數

  • NA

返回值

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

異常

  • NA

例子

下面的例子顯示lang.Byte.shortValue()方法的使用。

package com.yiibai;

import java.lang.*;

public class ByteDemo {

   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // create 2 short primitives s1, s2
      short s1, s2;

      // assign values to b1, b2
      b1 = new Byte("10");
      b2 = new Byte("-10");

      // assign short values of b1, b2 to s1, s2
      s1 = b1.shortValue();
      s2 = b2.shortValue();

      String str1 = "short value of Byte " + b1 + " is " + s1;
      String str2 = "short value of Byte " + b2 + " is " + s2;

      // print s1, s2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

讓我們來編譯和執行上面的程式,這將產生以下結果:

short value of Byte 10 is 10
short value of Byte -10 is -10