美文网首页
Java源码浅析,Short

Java源码浅析,Short

作者: Tomy_Jx_Li | 来源:发表于2018-09-17 23:52 被阅读24次

源码分析,基本上都加载注解上了,如有谬误,请指正,谢谢。
jdk1.8.0_161

public class Short extends Number implements Comparable<Short> {
    /**
     * 最小值,-2的15次方
     */
    public static final short   MIN_VALUE = -32768;

    /**
     * 最大值,2的15次方减1
     */
    public static final short   MAX_VALUE = 32767;

    /**
     * short的class对象,可以通过short.class获取
     */
    @SuppressWarnings("unchecked")
    public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");

    /**
     * 返回十进制的字符串形式,这里使用的就是Integer的方法,传入的可以是十六进制,8进制,10进制
     */
    public static String toString(short s) {
        return Integer.toString((int)s, 10);
    }

    /**
     * 将字符串,根据指定的基数,解析成十进制的short,主要还是调用的Integer的方法,传入的可以使十六进制,8进制,10进制
     */
    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;
    }

    /**
     * 将字符串,转换成十进制的short类型
     */
    public static short parseShort(String s) throws NumberFormatException {
        return parseShort(s, 10);
    }

    /**
     * 根据基数返回十进制的Short对象
     * 推荐使用这个方法返回Short对象
     */
    public static Short valueOf(String s, int radix)
            throws NumberFormatException {
        return valueOf(parseShort(s, radix));
    }

    /**
     * 根据字符串返回十进制的Short对象,
     * 推荐使用这个方法返回Short对象
     */
    public static Short valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

    /**
     * Short对象的缓存对象
     */
    private static class ShortCache {
        private ShortCache(){}

        /**
         * 缓存-128到127
         */
        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));
        }
    }

    /**
     * Short的自动装箱
     */
    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);
    }

    /**
     * 解码字符串为Short对象,可以解码的是十进制、16进制、8进制
     * 0x、0X和#代表16进制
     * 0开头代表8进制
     * 其他代表十进制
     *
     * <blockquote>
     * <dl>
     * <dt><i>DecodableString:</i>
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
     *
     */
    public static Short decode(String nm) throws NumberFormatException {
        // 这里其实也是使用的Integer的decode方法
        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对象中实际存储的short值
     */
    private final short value;

    /**
     * 构造器,每次调用返回的都是一个新的对象
     */
    public Short(short value) {
        this.value = value;
    }

    /**
     * 输入字符串,返回Short对象
     */
    public Short(String s) throws NumberFormatException {
        this.value = parseShort(s, 10);
    }

    /**
     * 返回此对象的byte值,返回方式一样。进行二进制截取
     */
    public byte byteValue() {
        return (byte)value;
    }

    /**
     * 返回short值
     */
    public short shortValue() {
        return 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;
    }

    /**
     * 返回十进制字符串形式
     */
    public String toString() {
        return Integer.toString((int)value);
    }

    /**
     * hashCode
     */
    @Override
    public int hashCode() {
        return Short.hashCode(value);
    }

    /**
     * 静态,hashCode,返回的是本身的值
     */
    public static int hashCode(short value) {
        return (int)value;
    }

    /**
     * equals
     */
    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占用的Byte数
     */
    public static final int BYTES = SIZE / Byte.SIZE;

    /**
     * 按照一个Byte位,也就是8位翻转指定值
     */
    public static short reverseBytes(short i) {
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
    }


    /**
     * 转成无符号的int,高16位用0占位
     */
    public static int toUnsignedInt(short x) {
        return ((int) x) & 0xffff;
    }

    /**
     * 转成无符号的long,高48位用0占位
     */
    public static long toUnsignedLong(short x) {
        return ((long) x) & 0xffffL;
    }

    /** use serialVersionUID from JDK 1.1. for interoperability */
    private static final long serialVersionUID = 7515723908773894738L;
}

相关文章

网友评论

      本文标题:Java源码浅析,Short

      本文链接:https://www.haomeiwen.com/subject/siijnftx.html