Short类

同样,该类继承Number类,实现Comparable(Short)接口。


属性:

public static final short MAX_VALUE = (short) 0x7FFF;

public static final short MIN_VALUE = (short) 0x8000;

public static final int SIZE = 16;

private final short value;

构造方法:

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

public Short(String string) throws NumberFormatException {
        this(parseShort(string));
    }

对象方法:

@Override
public byte byteValue() {
        return (byte) value;
}

public int compareTo(Short object) {
        return compare(value, object.value);
    }

@Override
public double doubleValue() {
        return value;
}

@Override
public boolean equals(Object object) {
        return (object instanceof Short) && (((Short) object).value == value);
}

@Override
public float floatValue() {
        return value;
}

@Override
public int hashCode() {
        return value;
}

@Override
public int intValue() {
        return value;
}

@Override
public long longValue() {
        return value;
}

@Override
public short shortValue() {
        return value;
}

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

类方法:

 private static final Short[] SMALL_VALUES = new Short[256];

static {
        for (int i = -128; i < 128; i++) {
            SMALL_VALUES[i + 128] = new Short((short) i);
        }
    }

public static Short valueOf(short s) {
        return s < -128 || s >= 128 ? new Short(s) : SMALL_VALUES[s + 128];
    }

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

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

public static Short valueOf(String string) throws NumberFormatException {
        return valueOf(parseShort(string));
    }

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

public static short parseShort(String string, int radix) throws NumberFormatException {
        int intValue = Integer.parseInt(string, radix);
        short result = (short) intValue;
        if (result == intValue) {
            return result;
        }
        throw new NumberFormatException("Value out of range for short: \"" + string + "\"");
    }

public static short parseShort(String string) throws NumberFormatException {
        return parseShort(string, 10);
    }

public static Short decode(String string) throws NumberFormatException {
        int intValue = Integer.decode(string).intValue();
        short result = (short) intValue;
        if (result == intValue) {
            return valueOf(result);
        }
        throw new NumberFormatException("Value out of range for short: \"" + string + "\"");
    }

public static int compare(short lhs, short rhs) {
        return lhs > rhs ? 1 : (lhs < rhs ? -1 : 0);
    }

@SuppressWarnings("unchecked")
public static final Class<Short> TYPE
            = (Class<Short>) short[].class.getComponentType();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值