Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Lớp Byte trong Java - Phương thức và ví dụ

Lớp Byte bao gói kiểu nguyên thủy byte (8-bit signed integer) thành đối tượng. Cung cấp các phương thức tiện ích để chuyển đổi, so sánh, parse và thao tác bit.

Các phương thức chính

STTPhương thức & Mô tả
1 byte byteValue() - Trả về giá trị byte của đối tượng này.
2 int compareTo(Byte anotherByte) - So sánh 2 Byte theo giá trị số.
3 static Byte decode(String nm) - Giải mã string thành Byte (hỗ trợ hex 0x, octal 0, decimal).
4 double doubleValue() - Trả về giá trị double.
5 boolean equals(Object obj) - So sánh bằng đối tượng khác.
6 float floatValue() - Trả về giá trị float.
7 int hashCode() - Mã băm của Byte.
8 int intValue() - Trả về giá trị int.
9 long longValue() - Trả về giá trị long.
10 static byte parseByte(String s) - Parse string decimal có dấu thành byte.
11 static byte parseByte(String s, int radix) - Parse string theo cơ số (2, 8, 10, 16...).
12 static Byte valueOf(String s) - Tạo Byte từ string decimal (cached -128~127).
13 static Byte valueOf(String s, int radix) - Tạo Byte từ string theo cơ số.
14 static Byte valueOf(byte b) - Tạo Byte từ byte primitive (cached -128~127).
15 String toString() - Biểu diễn chuỗi của Byte này.
16 static String toString(byte b) - Biểu diễn chuỗi của byte primitive.
17 static int toUnsignedInt(byte x) - Chuyển byte sang int unsigned (0-255) - Java 8+.
18 static long toUnsignedLong(byte x) - Chuyển byte sang long unsigned - Java 8+.
19 static int compare(byte x, byte y) - So sánh 2 byte primitive - Java 7+.
20 static int compareUnsigned(byte x, byte y) - So sánh unsigned - Java 9+.

Ví dụ 1: intValue(), toString()

public class Demo {
    public static void main(String[] args) {
        Byte b1 = new Byte("1");
        Byte b2 = new Byte("-1");
        
        int i1 = b1.intValue(); // 1
        int i2 = b2.intValue(); // -1
        
        System.out.println("int value of Byte " + b1 + " is " + i1);
        System.out.println("int value of Byte " + b2 + " is " + i2);
    }
}

Kết quả

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

Ví dụ 2: toString(), parseByte()

public class Demo {
    public static void main(String[] args) {
        Byte b1 = new Byte("-123");
        Byte b2 = new Byte("0");
        
        String s1 = b1.toString(); // "-123"
        String s2 = b2.toString(); // "0"
        
        System.out.println("String value of Byte " + b1 + " is " + s1);
        System.out.println("String value of Byte " + b2 + " is " + s2);
        
        // Parse
        byte parsed = Byte.parseByte("100"); // 100
        System.out.println("Parsed: " + parsed);
    }
}

Kết quả

String value of Byte -123 is -123
String value of Byte 0 is 0
Parsed: 100

Ví dụ 3: Xử lý unsigned byte (Java 8+)

public class UnsignedDemo {
    public static void main(String[] args) {
        // Byte signed max = 127 (0x7F)
        // Muốn lưu giá trị 200 (0xC8) -> overflow thành -56
        byte signed = (byte) 200; // -56
        
        System.out.println("Signed value: " + signed);                    // -56
        System.out.println("Unsigned int: " + Byte.toUnsignedInt(signed)); // 200
        System.out.println("Unsigned long: " + Byte.toUnsignedLong(signed)); // 200
        System.out.println("Hex: " + String.format("0x%02X", signed & 0xFF)); // 0xC8
        
        // So sánh unsigned
        byte b1 = (byte) 200; // -56 signed
        byte b2 = (byte) 100; // 100 signed
        System.out.println("Signed compare: " + Byte.compare(b1, b2));        // -1 (b1 < b2)
        System.out.println("Unsigned compare: " + Byte.compareUnsigned(b1, b2)); // 1 (200 > 100)
    }
}

Kết quả

Signed value: -56
Unsigned int: 200
Unsigned long: 200
Hex: 0xC8
Signed compare: -1
Unsigned compare: 1

Lưu ý quan trọng

  • Deprecated: Constructor new Byte(...) từ Java 9. Dùng Byte.valueOf(...).
  • Cache: Byte.valueOf(byte) cache -128 đến 127. So sánh bằng == chỉ đúng trong phạm vi cache.
  • Unsigned: Java không có kiểu unsigned byte nguyên thủy. Dùng toUnsignedInt hoặc & 0xFF khi xử lý binary/network.
  • Radix: parseByte("FF", 16) ném exception vì 255 > 127. Parse hex an toàn: (byte) Integer.parseInt("FF", 16).