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

Các trường (Fields) của lớp Byte trong Java

Lớp Byte là wrapper class bao gói kiểu nguyên thủy byte (8-bit signed integer). Lớp này định nghĩa các hằng số (static fields) quan trọng sau:

Các trường static của Byte

TrườngKiểuGiá trịMô tả
MAX_VALUE byte 127 (27 - 1) Giá trị lớn nhất byte có thể lưu (0x7F).
MIN_VALUE byte -128 (-27) Giá trị nhỏ nhất byte có thể lưu (0x80).
SIZE int 8 Số bit dùng để biểu diễn byte trong dạng bù 2 (two's complement).
BYTES int 1 Số byte (8-bit) - từ Java 8.
TYPE Class<Byte> Byte.class Đối tượng Class đại diện cho kiểu nguyên thủy byte.

Ví dụ 1: Sử dụng MIN_VALUE, intValue()

public class Demo {
    public static void main(String[] args) {
        Byte b1 = new Byte("1");
        Byte b2 = Byte.MIN_VALUE; // -128
        
        int i1 = b1.intValue(); // 1
        int i2 = b2.intValue(); // -128
        
        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 -128 is -128

Ví dụ 2: SIZE, toString()

public class Demo {
    public static void main(String[] args) {
        Byte b1 = new Byte("-10");
        Byte b2 = Byte.MIN_VALUE;
        
        System.out.println("Số bit của b1: " + b1.SIZE);     // 8
        System.out.println("Số bit của b2: " + b2.SIZE);     // 8
        System.out.println("Số byte (Java 8+): " + Byte.BYTES); // 1
        
        String s1 = b1.toString(); // "-10"
        String s2 = b2.toString(); // "-128"
        
        System.out.println("String value of Byte " + b1 + " is " + s1);
        System.out.println("String value of Byte " + b2 + " is " + s2);
    }
}

Kết quả

Số bit của b1: 8
Số bit của b2: 8
Số byte (Java 8+): 1
String value of Byte -10 is -10
String value of Byte -128 is -128

Các phương thức hữu ích khác của Byte

Phương thứcMô tả
byteValue()Trả về giá trị byte.
shortValue()Trả về giá trị short.
intValue()Trả về giá trị int.
longValue()Trả về giá trị long.
floatValue()Trả về giá trị float.
doubleValue()Trả về giá trị double.
compareTo(Byte)So sánh 2 Byte.
equals(Object)So sánh bằng.
hashCode()Mã băm.
parseByte(String)Parse string thành byte (static).
parseByte(String, int radix)Parse string theo cơ số (static).
valueOf(String)Tạo Byte từ string (static, cached).
valueOf(byte)Tạo Byte từ byte (static, cached -128 đến 127).
decode(String)Decode string (hỗ trợ 0x, #, 0 prefix).
toUnsignedInt(byte)Chuyển byte sang int không dấu (0-255) - Java 8+.
toUnsignedLong(byte)Chuyển byte sang long không dấu - Java 8+.

Ví dụ parseByte và decode

public class ParseDemo {
    public static void main(String[] args) {
        // parseByte: chỉ decimal, có dấu
        System.out.println(Byte.parseByte("100"));        // 100
        System.out.println(Byte.parseByte("-50"));        // -50
        // System.out.println(Byte.parseByte("FF"));      // NumberFormatException
        
        // decode: hỗ trợ hex (0x...), octal (0...), decimal
        System.out.println(Byte.decode("0x7F"));          // 127
        System.out.println(Byte.decode("#FF"));           // 255 -> lỗi! Byte max 127
        // Thực tế decode("FF") parse là 255 -> NumberFormatException vì vượt Byte range
        
        // valueOf: cached -128 đến 127
        Byte b1 = Byte.valueOf(100);
        Byte b2 = Byte.valueOf(100);
        System.out.println(b1 == b2); // true (cached)
        
        Byte b3 = Byte.valueOf(200); // lỗi compile: 200 vượt byte range
        // Phải ép: Byte.valueOf((byte) 200) -> -56 (overflow)
    }
}

Lưu ý

  • byte trong Java là signed (có dấu): -128 đến 127.
  • Khi làm việc với dữ liệu nhị phân/network (unsigned byte 0-255), dùng toUnsignedInt() hoặc & 0xFF.
  • Byte.valueOf() cache các giá trị -128 đến 127, nên dùng thay vì new Byte() (deprecated từ Java 9).
  • Từ Java 9, constructor new Byte(...) đã deprecated.