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