JDK源码解读第七章:java.lang.Short

598 阅读4分钟

Short

1.Short声明

public final class Long extends Number implements Comparable<Long>  {}

可以看到Short类继承了Number类,而又不是抽象类,自然要重写Number类中的xxxValue方法。另外Short类实现了Comparable接口,Comparable是一个接口,该接口定义类的自然顺序,实现该接口的类就可以按这种方式排序,一般情况下如果某类对象自身具有比较的特性就可以实现该接口,比如这里的Short代表的是一种数,而数本身就具有比较的特性,就可以实现该接口。

2.Short边界值

 	public static final short   MIN_VALUE = -32768;
    public static final short   MAX_VALUE = 32767;

最大值和最小值为什么是这么多,和上章Byte是一样的,只是short为16个字节,占位2位,最大值的补码为0111111111111111,转换为10进制就为32767,最小值补码为1000000000000000,十进制值为32768。

3.Short其他方法

    //基本类型 short 的 Class 实例,即short.class
    public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");
    //将short类型的数转换为其10进制对应的数的字符串形式
    public static String toString(short s) {
        return Integer.toString((int)s, 10);
    }
    //将short类型的数转换为其指定进制对应的数的字符串形式
    public static short parseShort(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 (short)i;
    }
    //将10进制的字符串数转换为对应的short类型的数字
    public static short parseShort(String s) throws NumberFormatException {
        return parseShort(s, 10);
    }
    //将指定进制的字符串数转换为对应的short类型的数字
    public static Short valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseShort(s, radix));
    }
    //获取字符串形式的数字,所对应的Short对象
    public static Short valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }
    //Short对象的缓冲类
    private static class ShortCache {
        private ShortCache(){}
        //缓冲数组
        static final Short cache[] = new Short[-(-128) + 127 + 1];
        //创建缓冲对象,共256个
        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Short((short)(i - 128));
        }
    }
    //从缓冲对象中获取对应的Short对象
    //可以看到在-127到128之间是从缓存数组中取的,如果所有范围数值都放在内存中其实比较消耗资源,所以和Byte一样只存放了256个数值。
    public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        if (sAsInt >= -128 && sAsInt <= 127) { // must cache
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }
    //将其它进制表示法,表示的字符串数据转换为10进制表示的short数据,然后返回对应的Short对象
    public static Short 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((short)i);
    }
    //用于存储short数值
    private final short value;
    //Short类的构造器,参数为short值
    public Short(short value) {
        this.value = value;
    }
    //Short类的构造器,参数为字符串
    public Short(String s) throws NumberFormatException {
        this.value = parseShort(s, 10);
    }
    //将short数转换为byte,由于short长度大于byte所以这里可以会出现溢出的情况
    public byte byteValue() {
        return (byte)value;
    }
    //返回short数据
    public short shortValue() {
        return value;
    }
    //将short数转换为int类型
    public int intValue() {
        return (int)value;
    }
    //将short数转换为long类型
    public long longValue() {
        return (long)value;
    }
    //将short数转换为float类型
    public float floatValue() {
        return (float)value;
    }
    //将short数转换为double类型
    public double doubleValue() {
        return (double)value;
    }
    //toString方法
    public String toString() {
        return Integer.toString((int)value);
    }
    public int hashCode() {
        return Short.hashCode(value);
    }
    public static int hashCode(short value) {
        return (int)value;
    }
    public boolean equals(Object obj) {
        if (obj instanceof Short) {
            return value == ((Short)obj).shortValue();
        }
        return false;
    }
    //比较两个short对象的大小
    public int compareTo(Short anotherShort) {
        return compare(this.value, anotherShort.value);
    }
    public static int compare(short x, short y) {
        return x - y;
    }
    //short类型所占的位数
    public static final int SIZE = 16;
    //short类型所占的字节数
    public static final int BYTES = SIZE / Byte.SIZE;
    //short的高8位与低8位互换
    public static short reverseBytes(short i) {
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
    }
    //将short类型的数转换为无符号的int类型
    public static int toUnsignedInt(short x) {
        return ((int) x) & 0xffff;
    }
    //将short类型的数转换为无符号的long类型
    public static long toUnsignedLong(short x) {
        return ((long) x) & 0xffffL;
    }
}

上述大部分方法源码其实和Byte的是一样的,不需要过多的说明,看看Byte的源码就清楚了。这里不在一一说明。

4.reverseBytes(short i)

 	public static short reverseBytes(short i) {
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
    }

这个方法在Byte中是不存在的,作用是将short类型的低8位与高8位互换的。
1.将目标数i与0xFF00(转换为2进制是 1111 1111 0000 0000)做&的运算,得到的结果为i的高8位不变,低8位全变为0,然后右移8位,就得到了目标数i的高8位
2.将i左移8位,得到的数为一个新数,这个新数的高8位为原目标数i的低8位,这个数的低8位为0
3.将i的高8位与这个新数做|运算,得到的结果就是将目标数i的高8位,与低8位互换的数。 比如一个数的二进制为1100 0011 0101 1010
1的运算结果 0000 0000 1100 0011
2的运算结果 0101 1010 0000 0000
3的最终结果 0101 1010 1100 0011