Byte源码解读.md

先看Number类:

public abstract class Number implements java.io.Serializable

它的方法列表如下:

intValue:init
longValue:long
floatValue:float
doubleValue:double

byteValue:byte
shortValue:short

除了byteValue()、shortValue(),其他都是抽象方法。

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

Byte.java


好了,接下来看Byte.java

public final class Byte extends Number implements Comparable

继承Number 实现Comparable接口

构造器:

成员变量value (很重要)

private final byte value;

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

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

parseByte(String s,int radix):byte

把字符串(仅限数字)解析为字节,但是当字符串s 的值 大于127,或者 小于-128时,会抛异常。

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

toString():

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

valueOf(byte b):Byte

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

注意:ByteCache是一个静态内部类,定义如下

private static class ByteCache {
    private ByteCache(){}

    static final Byte cache[] = new Byte[-(-128) + 127 + 1];//数组大小是256

    static {
    //依次填充 -128-------127
        for(int i = 0; i < cache.length; i++)
            cache[i] = new Byte((byte)(i - 128));
    }
}

valueOf还有两种重载形式

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

接下来就是实现Number抽象类中的方法:
其实都是把成员变量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;
}

继续,Comparable接口的方法实现:

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

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

我们接着看 equals hashcode toString 这3个方法:

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

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

public String toString() {
    return Integer.toString((int)value);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值