Java 中的 Byte 類


Byte 類將基本型別 byte 的值包裝在一個物件中。Byte 型別的物件包含一個型別為 byte 的單個欄位。

以下是 Byte 類的一些方法:

序號方法及描述
1byte byteValue()
此方法將此 Byte 的值作為 byte 返回。
2int compareTo(Byte anotherByte)
此方法按數值比較兩個 Byte 物件。
3static Byte decode(String nm)
此方法將字串解碼為 Byte。
4double doubleValue()
此方法將此 Byte 的值作為 double 返回。
5boolean equals(Object obj)
此方法將此物件與指定物件進行比較。
6float floatValue()
此方法將此 Byte 的值作為 float 返回。
7int hashCode()
此方法返回此 Byte 的雜湊碼。
8int intValue()
此方法將此 Byte 的值作為 int 返回。
9long longValue()
此方法將此 Byte 的值作為 long 返回。
10static byte parseByte(String s)
此方法將字串引數解析為帶符號的十進位制 byte。

現在讓我們來看一個例子:

示例

 即時演示

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      int i1, i2;
      b1 = new Byte("1");
      b2 = new Byte("-1");
      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 -1 is -1

示例

現在讓我們來看另一個例子:

 即時演示

import java.lang.*;
public class Demo {
   public static void main(String[] args){
      Byte b1, b2;
      String s1, s2;
      b1 = new Byte("-123");
      b2 = new Byte("0");
      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 );
   }
}

輸出

String value of Byte -123 is -123
String value of Byte 0 is 0

更新於: 2020年1月2日

817 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.