JDK1.8源码阅读(十二)Java.lang.Short

Short

定义

public final class Short extends Number implements Comparable<Short> {}

构造方法

属性

/*最小值 -2 的15次方*/
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类型的value*/
private final short value;

/*用于表示两个的短值的位数。补码二进制形式。*/
public static final int SIZE = 16;

/*表示短值的二进制形式字节数*/
public static final int BYTES = SIZE / Byte.SIZE;

常用方法

/*转String*/
public static String toString(short s) {
        return Integer.toString((int)s, 10);
    }

public String toString() {
        return Integer.toString((int)value);
    }

/*字符串带进制转Short,先使用Integer的parseInt方法转成int再强转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);
    }

/*将short返回成Short对象,如果范围在-128至127内就直接返回缓存里的数据,否则新实例化一个对象*/
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);
    }
/*调用下面的valueof方法*/
public static Short valueOf(String s) throws NumberFormatException {
        return valueOf(s, 10);
    }

/*调用parseShort方法,十进制*/
public static Short valueOf(String s, int radix)
        throws NumberFormatException {
        return valueOf(parseShort(s, radix));
    }
/*将解码信息转成Short对象*/
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);
    }

/*类型转换*/
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;
    }

/*返回哈希码*/
public int hashCode() {
        return Short.hashCode(value);
    }

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

....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值