คลาส Byte เป็น wrapper class สำหรับ primitive byte (8-bit signed two's complement integer) ห่อหุ้มค่า byte เดียวในออบเจ็กต์
Constructor (Deprecated since Java 9)
// Deprecated - ใช้ valueOf() แทน
Byte(byte value)
Byte(String s) throws NumberFormatException
แนะนำ: ใช้ Byte.valueOf(...) แทน เพื่อใช้ cached instances (-128 ถึง 127)
Instance Methods
| ลำดับ |
เมธอด |
คำอธิบาย |
| 1 |
byte byteValue() |
คืนค่า primitive byte ของ object นี้ |
| 2 |
int compareTo(Byte anotherByte) |
เปรียบเทียบสอง Byte objects numerically (implements Comparable) |
| 3 |
double doubleValue() |
คืนค่า double |
| 4 |
boolean equals(Object obj) |
เปรียบเทียบ object (true ถ้า obj เป็น Byte และมีค่าเท่ากัน) |
| 5 |
float floatValue() |
คืนค่า float |
| 6 |
int hashCode() |
คืน hash code (เท่ากับ int value) |
| 7 |
int intValue() |
คืนค่า int |
| 8 |
long longValue() |
คืนค่า long |
| 9 |
short shortValue() |
คืนค่า short |
| 10 |
String toString() |
คืน String representation (เช่น "10", "-128") |
Static Methods
| เมธอด |
คำอธิบาย |
static Byte decode(String nm) |
Decode string เป็น Byte (รองรับ decimal, hex 0x/0X/#, octal 0 prefix) |
static byte parseByte(String s) |
Parse signed decimal byte string |
static byte parseByte(String s, int radix) |
Parse string ใน radix ที่กำหนด (2-36) |
static Byte valueOf(String s) |
คืน Byte object จาก decimal string (cached -128 to 127) |
static Byte valueOf(String s, int radix) |
คืน Byte object จาก string ใน radix ที่กำหนด |
static Byte valueOf(byte b) |
คืน Byte object จาก primitive (cached -128 to 127) |
static String toString(byte b) |
Static method แปลง byte → String |
static String toUnsignedString(byte b) |
แปลง byte เป็น unsigned decimal string (0-255) |
static String toUnsignedString(byte b, int radix) |
แปลงเป็น unsigned string ใน radix ที่กำหนด |
static int toUnsignedInt(byte b) |
แปลง byte เป็น unsigned int (0-255) |
static int compare(byte x, byte y) |
เปรียบเทียบ primitive bytes (Java 7+) |
static int compareUnsigned(byte x, byte y) |
เปรียบเทียบเป็น unsigned (0-255) (Java 8+) |
ตัวอย่างที่ 1: ใช้ intValue() และ toString()
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(); // 1
i2 = b2.intValue(); // -1
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);
}
}
Output
int value of Byte 1 is 1
int value of Byte -1 is -1
ตัวอย่างที่ 2: ใช้ toString() และ valueOf()
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(); // "-123"
s2 = b2.toString(); // "0"
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);
}
}
Output
String value of Byte -123 is -123
String value of Byte 0 is 0
ตัวอย่างที่ 3: Static Methods และ Unsigned Operations
public class ByteStaticDemo {
public static void main(String[] args) {
// parseByte
byte b1 = Byte.parseByte("100");
byte b2 = Byte.parseByte("FF", 16); // -1 (signed)
// decode
Byte d1 = Byte.decode("0x7F"); // 127
Byte d2 = Byte.decode("#FF"); // -1
Byte d3 = Byte.decode("0200"); // -128 (octal)
// valueOf (cached)
Byte v1 = Byte.valueOf((byte) 50);
Byte v2 = Byte.valueOf("50");
System.out.println("Cached equal: " + (v1 == v2)); // true
// Unsigned operations (Java 8+)
byte b = (byte) 200; // -56 signed
System.out.println("Signed: " + b); // -56
System.out.println("Unsigned int: " + Byte.toUnsignedInt(b)); // 200
System.out.println("Unsigned string: " + Byte.toUnsignedString(b)); // "200"
System.out.println("Unsigned hex: " + Byte.toUnsignedString(b, 16)); // "C8"
// compareUnsigned
byte x = (byte) 200; // -56
byte y = (byte) 100; // 100
System.out.println("Signed compare: " + Byte.compare(x, y)); // -56 < 100 -> negative
System.out.println("Unsigned compare: " + Byte.compareUnsigned(x, y)); // 200 > 100 -> positive
}
}
การใช้งานจริง: Bit Manipulation และ I/O
import java.io.*;
import java.nio.ByteBuffer;
public class ByteIOExample {
public static void main(String[] args) throws IOException {
// เขียน bytes ลงไฟล์
byte[] data = {0x48, 0x65, 0x6C, 0x6C, 0x6F}; // "Hello" ใน ASCII
try (FileOutputStream fos = new FileOutputStream("test.bin")) {
fos.write(data);
}
// อ่าน bytes จากไฟล์
try (FileInputStream fis = new FileInputStream("test.bin")) {
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b); // Hello
}
}
// ByteBuffer สำหรับ binary data
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.put((byte) 0xFF);
buffer.putShort((short) 0x1234);
buffer.putInt(0x12345678);
System.out.println("\nBuffer bytes: ");
for (byte bt : buffer.array()) {
System.out.printf("%02X ", Byte.toUnsignedInt(bt));
}
}
}
สรุป: เมื่อไหร่ใช้ Byte vs byte
| สถานการณ์ |
แนะนำ |
| Primitive calculations, arrays, performance critical |
ใช้ byte (primitive) |
| Collections (List<Byte>, Map<String, Byte>) |
ใช้ Byte (wrapper) |
| Generic types, Reflection, Nullable values |
ใช้ Byte |
| Parsing strings, String conversion |
ใช้ Byte.parseByte(), Byte.valueOf() |
| Bitwise operations, I/O, Network protocols |
ใช้ byte + Byte.toUnsignedInt() |