Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

Byte Class Fields ใน Java (java.lang.Byte)

คลาส Byte เป็น wrapper class สำหรับ primitive type byte (8-bit signed integer) มี fields (constants) สำคัญ 4 ตัว:

Fields ของ Byte Class

Field Type ค่า คำอธิบาย
MAX_VALUE static byte 127 (2⁷-1) ค่าสูงสุดที่ byte เก็บได้
MIN_VALUE static byte -128 (-2⁷) ค่าต่ำสุดที่ byte เก็บได้
SIZE static int 8 จำนวน bits ใช้ represent byte (two's complement)
TYPE static Class<Byte> Byte.class Class object สำหรับ primitive byte

ตัวอย่างที่ 1: ใช้ MIN_VALUE และ intValue()

import java.lang.*;

public class Demo {
    public static void main(String[] args) {
        Byte b1, b2;
        int i1, i2;

        b1 = new Byte("1");
        b2 = Byte.MIN_VALUE; // -128

        i1 = b1.intValue(); // 1
        i2 = b2.intValue(); // -128

        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 -128 is -128

ตัวอย่างที่ 2: ใช้ SIZE และ toString()

import java.lang.*;

public class Demo {
    public static void main(String[] args) {
        Byte b1, b2;
        String s1, s2;

        b1 = new Byte("-10");
        b2 = Byte.MIN_VALUE; // -128

        System.out.println("Number of bits in b1 = " + b1.SIZE); // 8
        System.out.println("Number of bits in b2 = " + b2.SIZE); // 8

        s1 = b1.toString(); // "-10"
        s2 = b2.toString(); // "-128"

        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

Number of bits in b1 = 8
Number of bits in b2 = 8
String value of Byte -10 is -10
String value of Byte -128 is -128

Byte Class Methods สำคัญ (เพิ่มเติม)

เมธอด คำอธิบาย
byte byteValue() คืนค่า byte primitive
int compareTo(Byte anotherByte) เปรียบเทียบสอง Byte objects
static Byte decode(String nm) Decode string เป็น Byte (รองรับ hex, octal, decimal)
double doubleValue() คืนค่า double
boolean equals(Object obj) เปรียบเทียบ object
float floatValue() คืนค่า float
int hashCode() Hash code
int intValue() คืนค่า int
long longValue() คืนค่า long
static byte parseByte(String s) Parse string เป็น signed decimal byte
static byte parseByte(String s, int radix) Parse string ใน radix ที่กำหนด
static Byte valueOf(String s) คืน Byte object (cached -128 ถึง 127)
static Byte valueOf(byte b) คืน Byte object จาก primitive (cached)
static String toString(byte b) Static method แปลง byte → String
static String toUnsignedString(byte b) แปลงเป็น unsigned string (0-255)
static int toUnsignedInt(byte b) แปลงเป็น unsigned int (0-255)

ตัวอย่างเพิ่มเติม: parseByte, decode, toUnsignedString

public class ByteMethodsDemo {
    public static void main(String[] args) {
        // parseByte
        byte b1 = Byte.parseByte("100");        // 100
        byte b2 = Byte.parseByte("FF", 16);     // -1 (signed)
        byte b3 = Byte.parseByte("7F", 16);     // 127

        // decode (รองรับ prefix: 0x, 0X, #, 0)
        Byte d1 = Byte.decode("0xFF");  // -1
        Byte d2 = Byte.decode("#7F");   // 127
        Byte d3 = Byte.decode("0177");  // 127 (octal)

        // Unsigned operations (Java 8+)
        System.out.println("toUnsignedInt((byte)255): " + Byte.toUnsignedInt((byte)255)); // 255
        System.out.println("toUnsignedString((byte)255): " + Byte.toUnsignedString((byte)255)); // "255"
        System.out.println("toUnsignedString((byte)-1): " + Byte.toUnsignedString((byte)-1)); // "255"

        // valueOf (cached -128 to 127)
        Byte v1 = Byte.valueOf((byte)100);
        Byte v2 = Byte.valueOf((byte)100);
        System.out.println("Same cached object: " + (v1 == v2)); // true

        Byte v3 = Byte.valueOf((byte)200); // ไม่ได้ cache (out of range)
        Byte v4 = Byte.valueOf((byte)200);
        System.out.println("Same cached object: " + (v3 == v4)); // false (new objects)
    }
}

Two's Complement Representation

Byte ใช้ Two's Complement สำหรับ negative numbers:

Value   Binary (8-bit)    Unsigned
 127     0111 1111         127
   1     0000 0001         1
   0     0000 0000         0
  -1     1111 1111         255
 -128     1000 0000         128

เมธอด toUnsignedInt() และ toUnsignedString() ช่วย interpret byte เป็น unsigned (0-255)