Short源码

Short范围是包含在Integer的范围之内的,其中大部分的方法是一样的,也是调用Integer中的方法进行处理的,没有什么难度!!

package java.lang;
public final class Short extends Number implements Comparable<Short> {

    // Short 的最大值与最小值
    public static final short   MIN_VALUE = -32768;
    public static final short   MAX_VALUE = 32767;
    
    // Short的原始类型
    @SuppressWarnings("unchecked")
    public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");
    
    // 将short类型的数据转成String
    // 默认调用Integer.toString(), 不传基数,默认基数为10
    public static String toString(short s) {
        return Integer.toString((int)s, 10);
    }

    // 将数字字符串转化成Short数据,index为数字字符串s的基数
    public static short parseShort(String s, int radix)
        throws NumberFormatException {
        // 调用Integer.parseInt(),获取到返回的int数据后对其范围进行判断,是否在short范围之外
        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数据,不传入基数,默认基数为10
    public static short parseShort(String s) throws NumberFormatException {
        return parseShort(s, 10);
    }

    // 将数字字符串转化成Short数据,index为数字字符串s的基数
    public static Short valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseShort(s, radix));
    }
    
    // 将数字字符串转化成Short数据,不传入基数,默认基数为10
    public static Short valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }
    
    // 内部类Short的缓存
    private static class ShortCache {
        private ShortCache(){}
        static final Short cache[] = new Short[-(-128) + 127 + 1];
        static {
            // 初始化时将[-128, 127]缓存到cache[]中
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Short((short)(i - 128));
        }
    }
    
    // 获取到short数据,自动装箱时判断是否直接取数据还是新new对象
    public static Short valueOf(short s) {
        final int offset = 128;
        int sAsInt = s;
        // 判断范围是否在[-128, 127]之间,是就取缓存,不是就新new对象
        if (sAsInt >= -128 && sAsInt <= 127) {
            return ShortCache.cache[sAsInt + offset];
        }
        return new Short(s);
    }

    // 解码,将其它进制的nm字符串,转换成10进制的short数据
    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数据
    private final short value;
    
    // 将Short数据转成其它的数据类型
    public Short(short value) {
        this.value = value;
    }
    public Short(String s) throws NumberFormatException {
        this.value = parseShort(s, 10);
    }
    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;
    }
    
    // 将short转换成字符串
    public String toString() {
        return Integer.toString((int)value);
    }
    
    // 获取short变量撤销hashCode值
    @Override
    public int hashCode() {
        return Short.hashCode(value);
    }
    
    // 计算short类型的hashCode值,
    public static int hashCode(short value) {
        return (int)value;
    }
    
    // 比较两个short数据是否一样,参数必须是Short类型数据
    public boolean equals(Object obj) {
        if (obj instanceof Short) {
            return value == ((Short)obj).shortValue();
        }
        return false;
    }
    
    // 比较两个short数据,返回两个short的差值
    public int compareTo(Short anotherShort) {
        return compare(this.value, anotherShort.value);
    }
    
    // 比较两个short数据,返回两个short的差值
    public static int compare(short x, short y) {
        return x - y;
    }
    
    // short的最大长度
    public static final int SIZE = 16;
    // 
    public static final int BYTES = SIZE / Byte.SIZE;
    
    // 反转
    public static short reverseBytes(short i) {
        return (short) (((i & 0xFF00) >> 8) | (i << 8));
    }
    
    // 转换成无符号的int数据
    public static int toUnsignedInt(short x) {
        return ((int) x) & 0xffff;
    }
    
    // 转换成无符号的int数据
    public static long toUnsignedLong(short x) {
        return ((long) x) & 0xffffL;
    }

    private static final long serialVersionUID = 7515723908773894738L;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值