Short源码阅读(1.8)

153 阅读1分钟

基础数据结构的包装类,今天来看看整数Short的源码。日常编码中一般都用int,short还是用的比较少的。

类定义

public final class Short extends Number implements Comparable<Short>
public abstract class Number implements java.io.Serializable
public interface Comparable<T>

Short类不可继承。

成员变量

public static final short   MIN_VALUE = -32768;  //最小值 1000 0000 0000 0000
public static final short   MAX_VALUE = 32767; //最大值 0111 1111 1111 1111 
public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short"); //拿到short的class对象
private final short value;
public static final int SIZE = 16;
public static final int BYTES = SIZE / Byte.SIZE; //2
private static final long serialVersionUID = 7515723908773894738L;

cache

    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)); //-128----127
        }
    }

大名鼎鼎的缓存,想想为什么呢?

构造方法

    public Short(short value) {
        this.value = value;
    }
    public Short(String s) throws NumberFormatException {
        this.value = parseShort(s, 10);
    }
    public static short parseShort(String s, int radix)
        throws NumberFormatException {
        int i = Integer.parseInt(s, radix); //等看integer源码
        if (i < MIN_VALUE || i > MAX_VALUE)
            throw new NumberFormatException(
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
        return (short)i;
    }

toString

    public static String toString(short s) {
        return Integer.toString((int)s, 10); //又是借助integer
    }
    

valueOf

    public static Short valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseShort(s, radix));
    }
    public static Short valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }
    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);
    }

hashCode

    @Override
    public int hashCode() {
        return Short.hashCode(value);
    }
    public static int hashCode(short value) {
        return (int)value; //想想为什么可以直接返回value?什么叫做包装类?
    }

比较

    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;
    }

Number 父类方法

都是强转就不贴了

其他方法

    public static short reverseBytes(short i) {
        //0110 0100 0000 0000   --》   0000 0000 0110 0100
        //其实就是交换两个byte
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
    }
    public static int toUnsignedInt(short x) {
        //-32768  --》 32768    想想-1转换后是多少?
        return ((int) x) & 0xffff;
    }
    public static long toUnsignedLong(short x) {
        //-32768  --》 32768     想想-1转换后是多少?
        return ((long) x) & 0xffffL;
    }