JDK之Number类探密(1)

这2天开始潜心研习JDK1.5之源码,先从最简单的java.lang包看起。不看不知道,一看吓一跳,最简单的东东里面学问也深着呢。下面是我学习Number类的心得,留作笔记。

首先最顶层的类是Number,代表数字的抽象。

public abstract class Number implements java.io.Serializable

注意它实现了序列化。

Number的下面有这样一些类的实现:Byte,Short,Integer,Long,Float,Double,它们都是基本类型(primitive type)的包装器类,仅仅是首字母由小写变为大写。并且都实现了Compareable接口,这样就能比大小了(实现Compareable的另一个目的是为了排序)。

阅读代码心要静,写代码不容易,阅读别人代码也不简单。我大概花了3天才看完这6个类,体会到研究代码的不易。不过我们积少成多,不积跬步,无以至千里;不积小流,无以成江海。

首先从简单入手,研究Byte类,一边看代码,一边看注释,一边看javadoc。全部代码我就不贴了,有兴趣的自己看源码,没兴趣的贴了也不会看的。所以还是节省空间吧。仅把代码中的闪光点记录下来。

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





    private final byte value; //成员变量,定义成final,表明实例化后值不能再变了



   /**

     * byte 是8位的

     *

     * @since 1.5

     */

    public static final int SIZE = 8;



    /**

     * 一个常量,保存 byte 类型可取的最小值,即 -2^7

     */

    public static final byte MIN_VALUE = -128;



    /**

     * 一个常量,保存 byte 类型可取的最大值,即 2^7-1

     */

    public static final byte MAX_VALUE = 127;



    /**

     * 表示基本类型 byte 的 Class 实例

     */

    public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");



    //调用Class.getPrimitiveClass,是一个native方法,估计是用C++写的,以后再研究





    //内部类ByteCache 准备把所有256个byte存在缓存里

    private static class ByteCache {

        private ByteCache() {

        }



        //一个Byte数组,大小256

        static final Byte cache[] = new Byte[-(-128) + 127 + 1];



        //静态方法,类初始化时就会调用

        static {

            for (int i = 0; i < cache.length; i++)

                //256个Byte分别赋值(-128~127)

                cache[i] = new Byte((byte) (i - 128));

        }

    }



    public static Byte valueOf(byte b) {

        final int offset = 128;

        //可以调用private static class?注意直接拿缓存里的值了,比构造函数效率高

        return ByteCache.cache[(int) b + offset];

    }



    public static byte parseByte(String s, int radix)

            throws NumberFormatException {

        //居然是调用其他类 Integer.parseInt

        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;

    }



    /**

     * 将 String 解码为 Byte。接受十进制、十六进制和八进制数:

     */

    public static Byte decode(String nm) throws NumberFormatException {

        int radix = 10;

        int index = 0;

        boolean negative = false;

        Byte result;



        // 看第一个字符是否为"-",若是,符号标志置true,同时index加1

        if (nm.startsWith("-")) {

            negative = true;

            index++;

        }



        if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {

            //16进制

            index += 2;

            radix = 16;

        } else if (nm.startsWith("#", index)) {

            //16进制

            index++;

            radix = 16;

        } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {

            //8进制,011 是8进制,但0不算

            index++;

            radix = 8;

        }



        if (nm.startsWith("-", index))

            //出现2次负号可不行哦

            throw new NumberFormatException("Negative sign in wrong position");



        try {

            //注意这里返回范围 [0, 127]

            result = Byte.valueOf(nm.substring(index), radix);

            // 返回范围 [-127, 127] 少了个-128 被当作NumberFormatException例外了

            result = negative ? new Byte((byte) -result.byteValue()) : result;

        } catch (NumberFormatException e) {

            //特例,有可能是-128,重新解析一次

            String constant = negative ? new String("-" + nm.substring(index))

                    : nm.substring(index);

            result = Byte.valueOf(constant, radix);

        }

        return result;

    }



}

核心代码就这些了,是不是挺简单,增加了自信心。不过里面的Integer.parseInt,真正的核心还没有看到。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值