Java - Byte byteValue() 方法



描述

Java Byte byteValue() 方法返回此 Byte 的值作為 byte。

宣告

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

public byte byteValue()

重寫

Number 類中的 byteValue

引數

返回值

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

異常

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

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

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 byte primitive bt
      byte bt;

      // assign primitive value of b to bt
      bt = b.byteValue();

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

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

輸出

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

Primitive byte value of Byte object 100 is 100

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

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

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 byte primitive bt
      byte bt;

      // assign primitive value of b to bt
      bt = b.byteValue();

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

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

輸出

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

Primitive byte value of Byte object 100 is 100

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

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

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 byte primitive bt
      byte bt;

      // assign primitive value of b to bt
      bt = b.byteValue();

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

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

輸出

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

Primitive byte value of Byte object 100 is 100
java_lang_byte.htm
廣告
© . All rights reserved.