Byte

      byte是java基本数据类型之一,是java中长度最小的基本数字类型,并且我们在读写文件时经常使用byte数组,Byte是其包装类,现在我们一起去看看它的实现吧!

类图

Byte类图

public final class Byte extends Number implements Comparable<Byte>

      通过类图和源码我们可以知道Byte是不可被继承的,并且Byte的对象 是可以比较的,由于Byte继承了Number(这是一个抽象类),所以Byte重写了其所有形如xxxValue的方法。
Number类图

成员变量

public static final byte   MIN_VALUE = -128;
public static final byte   MAX_VALUE = 127;
public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");//获取Byte的class
private final byte value;
public static final int SIZE = 8;//表示二进制补码形式的byte值的位数
public static final int BYTES = SIZE / Byte.SIZE;//表示二进制补码形式的byte值的字节数

      Byte对象的值保持在value中,并且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 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;
}

      Byte有两个构造方法,一个是直接接受一个byte类型的值即可,将其赋值给value;另一个接受一个字符串类型数据(必须是数字字符串),通过parseByte方法将其转换,如果字符串为null或长度为0(如果首字符为-或+长度要大于1)则抛出NumberFormatException异常,传递的字符串值区间为[-128,127],超出此范围抛出NumberFormatException("Value out of range. Value:\"" + s + "\" Radix:" + radix)异常信息。

valueOf

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));
    }
}
public static Byte valueOf(byte b) {
    final int offset = 128;
    return ByteCache.cache[(int)b + offset];
}

      Byte实现了ByteCache这一静态内部类,不看代码也能猜测出此类由于缓存Byte数据,果不其然,cache[]存储了-128~127之间的所有数值。在使用valueOf获取Byte实例对象时都是获取cache[]中的对象(多次获取的都是同一个对象呀~),如获取-128,即cache[-128+128]=cache[0],使用offset这个技巧很巧妙呀!

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);
}

      java同时实现了通过string获取Byte对象的valueOf方法,我们仅仅需要注意的是字符串必须是合法的字符串,并且值在[-128,127]这个区间,其他的和上文的valueOf一致。

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);
}

      decode方法将字符串解码为Byte,此方法接受十进制字符串,十六进制字符串(用0x\0X\#标识),八进制字符串(用0标识),但是解码后的值必须在[-128,127]这一区间内,否则抛出NumberFormatException异常。

xxxValue

public byte byteValue() {
    return value;
}
public short shortValue() {
    return (short)value;
}
public float floatValue() {
    return (float)value;
}
public double doubleValue() {
    return (double)value;
}
public int intValue() {
    return (int)value;
}
public long longValue() {
    return (long)value;
}

      这六个方法都是继承自Number类,将value值强转为对应的类型。

hashCode

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

      Byte对象的hash值即为其value值。

equals

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

      首先判断obj是否为Byte的实例,再判断value是否相同。

compareTo

public int compareTo(Byte anotherByte) {
    return compare(this.value, anotherByte.value);
}
public static int compare(byte x, byte y) {
    return x - y;
}

      返回大于0前者大于后者,等于0两者相等,小于0前者小于后者。

toUnsignedInt

      jdk1.8后新增方法,将byte数据转换为无符号的int数据,也就是byte类型数据与0xff进行&操作后的值。与此方法类似的还有toUnsignedLong

Last modification:September 3rd, 2019 at 02:16 pm
如果觉得我的文章对你有用,请随意赞赏