JDK源码之Short类分析

一 简介

Short是short基本类型的包装类,同样继承了Number类,实现了Comparable接口:

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

Number传送门

二 源码分析(Short类源码比较简单)

属性和构造方法

    // 基本类型值
    private final short value;
    // 最小值 -2^15
    public static final short   MIN_VALUE = -32768;

    // 最大值 2^15-1
    public static final short   MAX_VALUE = 32767;

    //Class实例
    public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");

    // 二进制位数 16
    public static final int SIZE = 16;

    /**
     * @since 1.8  字节数 2
     */
    public static final int BYTES = SIZE / Byte.SIZE;
    
    //废弃的构造方法
    @Deprecated(since="9")
    public Short(short value) {
        this.value = value;
    }
    @Deprecated(since="9")
    public Short(String s) throws NumberFormatException {
        this.value = parseShort(s, 10);
    }

    //官方推荐的使用新的构造方法 (在[-128,127] 之间会优先使用缓存)
    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);
    }

    //将十进制String转换为Short
    public static Short valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

    //根据进制基数,将对应进制的String转换为Short
    public static Short valueOf(String s, int radix)
            throws NumberFormatException {
        return valueOf(parseShort(s, radix));
    }

    //根据进制解析String为short
    public static short parseShort(String s, int radix)
            throws NumberFormatException {
        int i = Integer.parseInt(s, radix); //调用Integer类的方法,详细逻辑可以看Integer类源码解析
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                    "Value out of range. Value:\"" + s + "\" Radix:" + radix);
        return (short)i;
    }

静态内部类

    //缓存静态内部类,和IntegerCaChe一样,范围写死为 [-128,127]
    private static class ShortCache {
        private ShortCache(){}

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

        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Short((short)(i - 128));
        }
    }

其他常用方法

    //静态toString
    public static String toString(short s) {
        return Integer.toString((int)s, 10);
    }

    /**
     * ecode合适用来分析数字
     * 可以分析
     * 8进:010=>分析后为 8
     * 10进:10=>分析后为 10
     * 16进:#10|0X10|0x10=>分析后是 16
     * 而valueof    只能分析纯数字的String
     * 像 010 这样的8进制 他会解析成 =>10
     */
    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);
    }

    // 调用Integer.toString方法
    public String toString() {
        return Integer.toString((int)value);
    }

    // hashCode为基本类型short的int值
    @Override
    public int hashCode() {
        return Short.hashCode(value);
    }

    /**
     * @since 1.8
     */
    public static int hashCode(short value) {
        return (int)value;
    }

    // 比较 基本类型short的值
    public boolean equals(Object obj) {
        if (obj instanceof Short) {
            return value == ((Short)obj).shortValue();
        }
        return false;
    }

    // 比较大小
    public int compareTo(Short anotherShort) {
        return compare(this.value, anotherShort.value);
    }

    //比较两个数大小
    public static int compare(short x, short y) {
        return x - y;
    }

    /**
     * @since 9  无符号值比较
     */
    public static int compareUnsigned(short x, short y) {
        return Short.toUnsignedInt(x) - Short.toUnsignedInt(y);
    }
    
    /**
     * 字节反转,即第一个和第二个字节反转
     */
    public static short reverseBytes(short i) {
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
    }


    /**
     * @since 1.8 转化为无符号int
     */
    public static int toUnsignedInt(short x) {
        return ((int) x) & 0xffff;
    }

    // @since 1.8 转化为无符号long
    public static long toUnsignedLong(short x) {
        return ((long) x) & 0xffffL;
    }

    private static final long serialVersionUID = 7515723908773894738L;
posted @ 2020-01-25 16:39  侯小厨  阅读(339)  评论(0编辑  收藏  举报
Fork me on Gitee