Java - Byte intValue() 方法



描述

Java Byte intValue() 方法返回此 Byte 作為 int 的值。

宣告

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

public int intValue()

指定於

Number 類中的 intValue

引數

返回值

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

異常

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

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

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 int primitive d
      int d;

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

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

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

輸出

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

Primitive int value of Byte object 100 is 100

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

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

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 int primitive d
      int d;

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

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

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

輸出

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

Primitive int value of Byte object 100 is 100

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

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

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 int primitive d
      int d;

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

      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
廣告