Java.lang.Byte源码

package java.lang

  • @since JDK1.1

public final class Byte extends Number implements Comparable< Byte> {

ByteCache缓冲区

通过valueOf()方法返回的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的上下区间MAX_VALUE、MIN_VALUE

public static final byte MIN_VALUE = -128

public static final byte MAX_VALUE = 127

private final byte value

构造方法

    public Byte(byte value) {
        this.value = value;
    }

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

成员方法

valueOf

    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

parseByte和valueOf

1,将String转换为Byte,默认十进制
2,valueOf是通过调用parseByte()实现函数功能

    public static byte parseByte(String s, int radix)
        throws NumberFormatException {
        int i = Integer.parseInt(s, radix);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
        return (byte)i;
    }

    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

    public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

decode

    public static Byte decode(String nm) throws NumberFormatException {
        int i = Integer.decode(nm);
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                "Value " + i + " out of range from input " + nm);
        return valueOf((byte)i);
    }

[外链图片转存失败(img-UbJwCsYn-1566206461853)(…/…/img/ByteEncodeAndValueOF.png)]
[外链图片转存失败(img-47cmIvFH-1566206461860)(…/…/img/ByteEncodeAndValueOFResult.png)]

  1. decode()parseByte()都是用于将String转换为Byte,区别在于decode可以解析八进制、十进制、十六进制的String,而valueOf只能解析数字字面量
  2. decode通过调用valueOf()实现函数功能,关键在于第一步的String转换为int,感兴趣的可以翻看我的Integer源码解析文章//TODO

获取Value

    public byte byteValue() {
        return value;
    }
    public short shortValue() {
        return (short)value;
    }

    public int intValue() {
        return (int)value;
    }

    public long longValue() {
        return (long)value;
    }

    public float floatValue() {
        return (float)value;
    }

    public double doubleValue() {
        return (double)value;
    }

toString()

    public static String toString(byte b) {
        return Integer.toString((int)b, 10);
    }

    public String toString() {
        return Integer.toString((int)value);
    }

hashCode()和equals()

    @Override
    public int hashCode() {
        return Byte.hashCode(value);
    }

    public static int hashCode(byte value) {
        return (int)value;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }

compareTo

    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

    public static int compare(byte x, byte y) {
        return x - y;
    }

toUnsignedInt()和toUnsignedInt()

    public static int toUnsignedInt(byte x) {
        return ((int) x) & 0xff;
    }

    public static long toUnsignedLong(byte x) {
        return ((long) x) & 0xffL;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值