JDK 源码:Byte

Byte 是 Java 中表示字节的包装类。它提供了将字节转换为其他基本数据类型的方法,以及将其他基本数据类型转换为字节的方法。以下是 JDK 8 中 Byte 类的源码解析:

package java.lang;

import java.io.Serializable;

public final class Byte extends Number implements Comparable<Byte> {
    // 序列化 ID
    private static final long serialVersionUID = -7183698231559129828L;

    // Byte 的常量
    public static final byte MIN_VALUE = -128;
    public static final byte MAX_VALUE = 127;
    public static final int SIZE = 8;
    public static final int BYTES = SIZE / Byte.SIZE;

    // 实际存储字节值的字段
    private final byte value;

    // 构造函数
    public Byte(byte value) {
        this.value = value;
    }

    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }

    // 字节解析方法
    public static byte parseByte(String s, int radix) throws NumberFormatException {
        int intValue = Integer.parseInt(s, radix);
        if (intValue < MIN_VALUE || intValue > MAX_VALUE) {
            throw new NumberFormatException("Value out of range for byte: " + intValue);
        }
        return (byte) intValue;
    }

    // 字节解析方法,默认按十进制解析
    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

    // 返回字节值的字符串表示
    public String toString() {
        return Integer.toString((int) value);
    }

    // 返回字节值的哈希码
    public int hashCode() {
        return Byte.hashCode(value);
    }

    // 字节值的静态哈希码计算方法
    public static int hashCode(byte value) {
        return (int) value;
    }

    // 比较两个字节值
    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

    // 比较两个字节值的静态方法
    public static int compare(byte x, byte y) {
        return x - y;
    }

    // 判断两个 Byte 对象是否相等
    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte) obj).byteValue();
        }
        return false;
    }

    // 将字符串解析为 Byte 对象
    public static Byte valueOf(String s, int radix) throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

    // 将字符串解析为 Byte 对象,默认按十进制解析
    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

    // 将字节值转换为 Byte 对象
    public static Byte valueOf(byte b) {
        return ByteCache.cache[b + 128];  // 自动装箱的缓存处理
    }

    // 内部类,用于缓存 Byte 对象
    private static class ByteCache {
        private ByteCache() {}

        // 自动装箱缓存
        static final Byte cache[] = new Byte[-(-128) + 127 + 1];

        static {
            for (int i = 0; i < cache.length; i++) {
                cache[i] = new Byte((byte) (i - 128));
            }
        }
    }

    // 将字符串解析为 Byte 对象
    public static Byte decode(String nm) throws NumberFormatException {
        int intValue = Integer.decode(nm);
        if (intValue < MIN_VALUE || intValue > MAX_VALUE) {
            throw new NumberFormatException("Value out of range for byte: " + intValue);
        }
        return valueOf((byte) intValue);
    }

    // 字节转换为无符号整数
    public static int toUnsignedInt(byte x) {
        return ((int) x) & 0xff;
    }

    // 字节转换为无符号长整数
    public static long toUnsignedLong(byte x) {
        return ((long) x) & 0xffL;
    }

    // 字节转换为字符串
    public static String toString(byte b) {
        return Integer.toString((int) b);
    }

    // 字节转换为字符串,指定基数
    public static String toString(byte b, int radix) {
        return Integer.toString((int) b, radix);
    }

    // 字节转换为无符号十六进制字符串
    public static String toHexString(byte b) {
        return Integer.toHexString((int) b & 0xff);
    }

    // 字节转换为无符号八进制字符串
    public static String toOctalString(byte b) {
        return Integer.toOctalString((int) b & 0xff);
    }

    // 字节转换为无符号二进制字符串
    public static String toBinaryString(byte b) {
        return Integer.toBinaryString((int) b & 0xff);
    }
}

主要功能解析:

  1. 常量

    • MIN_VALUEMAX_VALUE:表示 Byte 类型的最小值和最大值。
    • SIZE:表示 Byte 类型的位数。
    • BYTES:表示 Byte 类型的字节数。
  2. 构造函数

    • Byte(byte value):通过给定的字节值初始化。
    • Byte(String s):通过字符串解析为字节值初始化。
  3. 字节解析

    • parseByte(String s, int radix):将字符串解析为字节值,可以指定解析的进制。
    • parseByte(String s):将字符串按十进制解析为字节值。
  4. 字节值转换为字符串

    • toString():返回字节值的字符串表示。
  5. 哈希码

    • hashCode():返回字节值的哈希码。
  6. 字节值比较

    • compareTo(Byte anotherByte):比较当前字节对象与另一个字节对象的值。
    • compare(byte x, byte y):静态方法,比较两个字节值。
  7. 字节对象相等判断

    • equals(Object obj):判断当前字节对象与另一个对象是否相等。
  8. 字符串转换为字节对象

    • valueOf(String s, int radix):将字符串解析为字节对象,可以指定解析的进制。
    • valueOf(String s):将字符串按十进制解析为字节对象。
    • valueOf(byte b):将字节值转换为字节对象。
  9. 字节缓存

    • ByteCache 内部类用于缓存字节对象,避免重复创建对象,提高性能。
  10. 其他转换方法

  • decode(String nm):将字符串解析为字节对象。
  • toUnsignedInt(byte x)toUnsignedLong(byte x):将有符号字节转换为无符号整数和长整数。
  • toString(byte b, int radix)toHexString(byte b)toOctalString(byte b)toBinaryString(byte b):将字节值转换为字符串表示,可以指定不同的进制。

Byte 类提供了丰富的方法来处理字节数据,方便了在 Java 中对字节的操作。

  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值