Java 中的 Byte 類
Byte 類將基本型別 byte 的值包裝在一個物件中。Byte 型別的物件包含一個型別為 byte 的單個欄位。
以下是 Byte 類的一些方法:
| 序號 | 方法及描述 |
|---|---|
| 1 | byte byteValue() 此方法將此 Byte 的值作為 byte 返回。 |
| 2 | int compareTo(Byte anotherByte) 此方法按數值比較兩個 Byte 物件。 |
| 3 | static Byte decode(String nm) 此方法將字串解碼為 Byte。 |
| 4 | double doubleValue() 此方法將此 Byte 的值作為 double 返回。 |
| 5 | boolean equals(Object obj) 此方法將此物件與指定物件進行比較。 |
| 6 | float floatValue() 此方法將此 Byte 的值作為 float 返回。 |
| 7 | int hashCode() 此方法返回此 Byte 的雜湊碼。 |
| 8 | int intValue() 此方法將此 Byte 的值作為 int 返回。 |
| 9 | long longValue() 此方法將此 Byte 的值作為 long 返回。 |
| 10 | static 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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP