位元組類在 Java 中包含示例


位元組類將一個基本型別位元組的值封裝在物件中。

以下是位元組類的欄位:

  • static byte MAX_VALUE − 這是儲存位元組能達到的最大值(27-1)的常量。
  • static byte MIN_VALUE − 這是儲存位元組能達到的最小值(-27)的常量。
  • static int SIZE − 這是以二進位制補碼形式表示位元組值所需的位數。
  • static Class<Byte> TYPE − 這是表示基本型別位元組的類例項。

示例

現在讓我們看一個示例:

 即時演示

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      int i1, i2;
      b1 = new Byte("1");
      b2 = Byte.MIN_VALUE;
      i1 = b1.intValue();
      i2 = b2.intValue();
      String str1 = "int value of Byte " + b1 + " is " + i1;
      String str2 = "int value of Byte " + b2 + " is " + i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

這將產生以下輸出:

int value of Byte 1 is 1
int value of Byte -128 is -128

示例

現在讓我們看另一個示例:

 即時演示

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      String s1, s2;
      b1 = new Byte("-10");
      b2 = Byte.MIN_VALUE;
      System.out.println("Number of bits in b1 = "+b1.SIZE );
      System.out.println("Number of bits in b2 = "+b2.SIZE );
      s1 = b1.toString();
      s2 = b2.toString();
      String str1 = "String value of Byte " + b1 + " is " + s1;
      String str2 = "String value of Byte " + b2 + " is " + s2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

這將產生以下輸出:

Number of bits in b1 = 8
Number of bits in b2 = 8
String value of Byte -10 is -10
String value of Byte -128 is -128

更新於: 2020 年 1 月 2 日

112 次檢視

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.