JavaSourceLearning-java.lang.Short

有一说一,这周末是过的真的快!睡个懒觉,起来就只剩半天了,啥事没干又到晚上了!在这月黑风高,寂静无人的大晚上,不得看点源码提提神?我们来瞄一眼Short类。

image.png

Short

Short 类将原始类型 short 的值包装在对象中。 Short 类型的对象包含一个 short 类型的字段。 此外,该类提供了几种将 short 转换为String 和 String 转换为short 的方法,以及在处理 short 时有用的其他常量和方法。

image.png

public final class Short extends Number implements Comparable<Short>{}
复制代码

与 Byte 类类似,Short 类继承了 Number 类并实现了 Comparable 接口,但被final修饰,不可被其它类继承。

一、类中常量及变量

//short所能存储的最小值
public static final short   MIN_VALUE = -32768;
//short所能存储的最大值
public static final short   MAX_VALUE = 32767;
//Short所包装的值,在创建对象时赋值。
private final short value;
//用于以二进制补码形式表示 short 的位数。即16位表示一个short值。
public static final int SIZE = 16;
//用于以二进制补码形式表示 short 的字节数。即需2个字节表示一个short值。
public static final int BYTES = SIZE / Byte.SIZE;
//用于序列化
private static final long serialVersionUID = 7515723908773894738L;
复制代码

二、类中构造方法

public Short(short value) {
    this.value = value;
}

public Short(String s) throws NumberFormatException {
    this.value = parseShort(s, 10);
}
复制代码

类似于 Byte 类,无无参构造方法,意味着创建 Short 对象时,必须初始化被包装的 short 值,否则无法通过编译。第二个构造方法调用的是本类中的parseShort方法。代码如下:

    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;
    }
复制代码

Byte 类中的 parseByte 方法不能说一摸一样,只能说别无二致。实际调用的是 Integer 类中的parseInt 方法,作用是将字符串s转换为指定进制的int值,此处就是将字符串s转换为10进制的int值。然后在 parseShort() 中判断是否超出 short 所能表示的范围,是则抛出异常,否则将返回的int值强转为short类型并赋值给类中的 value。

三、类中相关方法

    //表示原始类型 short 的 Class 实例。
    @SuppressWarnings("unchecked")
    public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");
复制代码

parseShort

将制定字符串转换为10进制表示的int值,而后判断是否超过short表示的范围,最后强转为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;
    }
    
    public static short parseShort(String s) throws NumberFormatException {
        return parseShort(s, 10);
    }
复制代码

valueOf

从代码中可以看出,valueOf()先调用了parseShort()将String s转化为short s,而后判断值是否在-128~127之间,在则直接返回以缓存的Short对象,不在则新建该对象。

    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);
    }
复制代码

内部类ShortCache

类似Byte的内部类,缓存了[-128,127]的Short对象。

    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));
        }
    }
复制代码

decode

将 String 解码为 Short。 实际依赖于Integer类中的decode方法。

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

复制代码

xxxValue

将short值强转为各个类型的值。

    public byte byteValue() {
        return (byte)value;
    }
    
    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;
    }
复制代码

reverseBytes

    //通过反转指定short值的二进制补码表示中的字节顺序而获得的值,实际就是将short类型的低8位与高8位互换
    public static short reverseBytes(short i) {
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
    }
复制代码

toUnsignedXxx

    //将short转换为无符号整型
    public static int toUnsignedInt(short x) {
        return ((int) x) & 0xffff;
    }
    //将short转换为无符号long
    public static long toUnsignedLong(short x) {
        return ((long) x) & 0xffffL;
    }

复制代码

重写或实现相关方法

    
    public String toString() {
        return Integer.toString((int)value);
    }
    
    public static String toString(short s) {
        return Integer.toString((int)s, 10);
    }
    
    @Override
    public int hashCode() {
        return Short.hashCode(value);
    }

    
    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;
    }
复制代码

总的来说,Short类大多类似Byte类,且有较多方法依赖Integer类。较为简单。

该博客仅为初学者自我学习的记录,粗浅之言,如有不对之处,恳请指正。

Guess you like

Origin juejin.im/post/7054147009908834334