同样,该类继承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();