原始类型包装类解读(Short)

该类继承自Number,仅有一个final short类型的成员变量value,用来保存对应的原始类型。

静态常量

    public static final short   MIN_VALUE = -32768;
    public static final short   MAX_VALUE = 32767;
    public static final Class<Short> TYPE = (Class<Short>) short[].class.getComponentType();
    //占16bit
    public static final int SIZE = 16;
    //占2Byte
    public static final int BYTES = SIZE / Byte.SIZE;

构造函数

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

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

ShortCache

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

该类缓存了-128~127共256个Short值。

parseShort

    public static short parseShort(String s) throws NumberFormatException {
        return parseShort(s, 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;
    }

valueOf

    public static Short valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

    public static Short valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseShort(s, radix));
    }

    public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        if (sAsInt >= -128 && sAsInt <= 127) { // 从缓存取
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }

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

学习Integer时再细说。

reverseBytes

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

将高8位和低8位的bit值互换。比如原值为0x0F00,则互换后为0x000F。

toUnsignedInt

    public static int toUnsignedInt(short x) {
        return ((int) x) & 0xffff;
    }

转换为无符号整型。

toUnsignedLong

    public static long toUnsignedLong(short x) {
        return ((long) x) & 0xffffL;
    }

转换为无符号长整型。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • byte即字节的意思,java中的基本类型之一、也是java中长度最小的基本数字类型,通常在读取文件时需要用字节数...
    Kinsanity阅读 1,282评论 0 5
  • 该类表示Unicode字符,仅有一个final char类型的成员变量value,用来保存对应的原始类型。 静态常...
    sollian阅读 661评论 0 0
  • 该类继承自Number,仅有一个final byte类型的成员变量value,用来保存对应的原始类型。 静态常量 ...
    sollian阅读 566评论 0 0
  • Java8张图 11、字符串不变性 12、equals()方法、hashCode()方法的区别 13、...
    Miley_MOJIE阅读 3,867评论 0 11
  • 倘若我路过白桦枫林,不曾褪却那一身金黄,是否会于这执手秋叶中,读出那肆无忌惮的思念和因爱成恨的痴怨。以至于在寒冬迷...
    不醉怎能入睡阅读 254评论 0 1

友情链接更多精彩内容