JDK之Byte源码解析

刚入java不久的程序猿,对于简单的使用已毫不满足,最终为了一探究竟,翻开了JDK的源码,以下观点为自己的理解及看了多篇博客的总结,欢迎各位大神指出不对的地方,当然也欢迎和我一样刚学的同学,一起加油努力吧~~

Byte源码解析

今天来看一下Byte的源码,总体来说也比较简单,下面直接附上源码

/**
 * Byte提供相应的转换方法
 * Byte继承抽象类Number,实现了抽象类的方法,该抽象类多数方法都是用户byte转换为其他基础类型的方法
 * Byte实现Comparable接口,对接口里的比较方法进行了实现
 */
public final class Byte extends Number implements Comparable<Byte> {

    /**
     * byte的最小值为-128
     */
    public static final byte   MIN_VALUE = -128;

    /**
     * byte最大值为127
     */
    public static final byte   MAX_VALUE = 127;

    /**
     * 获取byte的字节码
     */
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");

    /**
     * 调用Integer的toString方法将byte值转换为String类型
     */
    public static String toString(byte b) {
        return Integer.toString((int)b, 10);
    }

    /**
     * 当加载Byte类时进行加载,将-128到127加载入内存中,使其唯一
     */
    private static class ByteCache {
        private ByteCache(){}
        //这是为了表名是从-128到127,后面的1位是数字0
        static final Byte cache[] = new Byte[-(-128) + 127 + 1];
        //静态代码块,类加载时加载,初始化cache中元素
        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));
        }
    }

    /**
     * byte类型转换为Byte类型,返回值直接从内存中读取
     */
    public static Byte valueOf(byte b) {
        final int offset = 128;
        return ByteCache.cache[(int)b + offset];
    }

    /**
     * 将字符串类型值转换为byte类型,转换失败抛出转换异常
     * 调用Integer的转换方法,将其转换为int类型后判断是否在byte区间内,最后返回byte值
     */
    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;
    }

    /**
     * 调用上方parseByte方法,将String类型值转换为byte类型
     */
    public static byte parseByte(String s) throws NumberFormatException {
        return parseByte(s, 10);
    }

    /**
     * 调用parseByte和valueOf方法,将String类型值转换为Byte类型
     */
    public static Byte valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseByte(s, radix));
    }

    /**
     * 调用valueOf方法,将String类型值转换为Byte
     */
    public static Byte valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

    /**
     * 将字符串转换为字节
     */
    public static Byte decode(String nm) throws NumberFormatException {
        int i = Integer.decode(nm);
        //转换后不在区间内则抛出异常,否则返回Byte值
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                    "Value " + i + " out of range from input " + nm);
        return valueOf((byte)i);
    }

    /**
     * Byte类型的值
     */
    private final byte value;

    /**
     * Byte无参构造,用于创建一个Byte对象
     */
    public Byte(byte value) {
        this.value = value;
    }

    /**
     * Byte有参构造,传入String类型参数,用于创建Byte对象
     */
    public Byte(String s) throws NumberFormatException {
        this.value = parseByte(s, 10);
    }

    /**
     * 获得当前Byte的值
     */
    public byte byteValue() {
        return value;
    }

    /**
     * 重写父类方法,将byte值转换为short类型
     */
    public short shortValue() {
        return (short)value;
    }

    /**
     * 重写父类方法,将byte值转换为int类型
     */
    public int intValue() {
        return (int)value;
    }

   /**
     * 重写父类方法,将byte值转换为int类型
     */
    public long longValue() {
        return (long)value;
    }

    /**
     * 重写父类方法,将byte值转换为float类型
     */
    public float floatValue() {
        return (float)value;
    }

    /**
     * 重写父类方法,将byte值转换为double类型
     */
    public double doubleValue() {
        return (double)value;
    }

    /**
     * 调用Integer的toString方法,将值转换为String类型
     */
    public String toString() {
        return Integer.toString((int)value);
    }

    /**
     * 计算哈希值
     */
    public int hashCode() {
        return (int)value;
    }

    /**
     * 用于对象的比较
     */
    public boolean equals(Object obj) {
        //当obj为Byte类型时,比较value值是否相等
        if (obj instanceof Byte) {
            return value == ((Byte)obj).byteValue();
        }
        return false;
    }

    /**
     * 调用下方compare方法,用于比较值是否相等
     */
    public int compareTo(Byte anotherByte) {
        return compare(this.value, anotherByte.value);
    }

    /**
     * 当x<y时,返回值-1
     * 当x=y时,返回值0
     * 当x>y时,返回值1
     */
    public static int compare(byte x, byte y) {
        return x - y;
    }

    /**
     * 用于表示二进制byte值的大小,为8位
     */
    public static final int SIZE = 8;

    /** 版本号 */
    private static final long serialVersionUID = -7183698231559129828L;
}

这里对Byte的源码看了一番,觉得比较有价值的就是弄清区间以及在类加载时候,会将-128到127的值初始化到内存中去。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值