说明:
Short类在一个对象中包含一个基本类型short的值。 类型为Short的对象包含一个类型为short的单个字段。此外,该类还提供了几种将short转换为String和String转换为short ,以及在处理short时有用的其他常数和方法。short 数据类型是 16 位、有符号的以二进制补码表示的整数。
类图结构:
源码解析:
package java.lang;
public final class Short extends Number implements Comparable<Short> {
/**
* 最小值是 -32768(-2^15);
*/
public static final short MIN_VALUE = -32768
/**
* 最大值是 32767(2^15 - 1)
*/
public static final short MAX_VALUE = 32767;
/**
* 类原始类型short的 类实例。
*/
@SuppressWarnings("unchecked")
public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short");
/**
* 将short类型转化成String。
*/
public static String toString(short s) {
return Integer.toString((int)s, 10);
}
/**
* 将字符串参数解析为由第二个参数指定的基数中的带符号的short。第一个参数是待转换的字符串,第二个参数表示进制数,这里的转换其实是调 了Integer的parseInt方法,返回值再判断是不是在short的最小值和最大值之间。如:Short.parseShort("1000",2)表示二进制的100,所以值为8。
*/
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;
}
/**
* 将字符串参数解析为带符号的十进制short 。
*/
public static short parseShort(String s) throws NumberFormatException {
return parseShort(s, 10);
}
/**
* 返回一个 Short对象持有价值从指定的String解析时由第二个参数给定的基数提取。
*/
public static Short valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseShort(s, radix));
}
/**
* 返回一个 Short对象持有价值由指定的String给定。
*/
public static Short valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
/**
* 静态内部类ShortCache,由于short取值范围太大,只缓存Byte的取值范围-128——127的256个值。
*/
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
/**
* 将short包装成Short
* @since 1.5
*/
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);
}
/**
* 将String解码成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);
}
private final short value;
/**
* 构建了一个新分配的 Short表示指定的short价值。
*/
public Short(short value) {
this.value = value;
}
/**
* 构建了一个新分配的 Short表示 short值表示的 String参数。
*/
public Short(String s) throws NumberFormatException {
this.value = parseShort(s, 10);
}
/**
* 将value转化成基本byte类型
*/
public byte byteValue() {
return (byte)value;
}
/**
* 将value转化成基本short类型
*/
public short shortValue() {
return value;
}
/**
* 将value转化成基本int类型
*/
public int intValue() {
return (int)value;
}
/**
* 将value转化成基本long类型
*/
public long longValue() {
return (long)value;
}
/**
* 将value转化成基本float类型
*/
public float floatValue() {
return (float)value;
}
/**
* 将value转化成基本double类型
*/
public double doubleValue() {
return (double)value;
}
/**
* 将value转化成基本String类型
*/
public String toString() {
return Integer.toString((int)value);
}
/**
* 返回此 Short哈希代码;等于调用 intValue()结果。
*/
@Override
public int hashCode() {
return Short.hashCode(value);
}
/**
* 返回一个 short值的哈希代码兼容 Short.hashCode()。
* @since 1.8
*/
public static int hashCode(short value) {
return (int)value;
}
/**
* 将此对象与指定的对象进行比较。
*/
public boolean equals(Object obj) {
if (obj instanceof Short) {
return value == ((Short)obj).shortValue();
}
return false;
}
/**
* 比较两 Short对象数值。
* @since 1.2
*/
public int compareTo(Short anotherShort) {
return compare(this.value, anotherShort.value);
}
/**
* 比较两 short值的数值大小。
* @since 1.7
*/
public static int compare(short x, short y) {
return x - y;
}
/**
* 用于二进制补码形式表示 short值的比特数。
* @since 1.5
*/
public static final int SIZE = 16;
/**
* The number of bytes used to represent a {@code short} value in two's
* complement binary form.
*
* @since 1.8
*/
public static final int BYTES = SIZE / Byte.SIZE;
/**
* 返回反转指定的二进制补码表示的字节顺序而获得的值 short值。
* @since 1.5
*/
public static short reverseBytes(short i) {
return (short) (((i & 0xFF00) >> 8) | (i << 8));
}
/**
* 将short转换成无符号int。
* @since 1.8
*/
public static int toUnsignedInt(short x) {
return ((int) x) & 0xffff;
}
/**
* 将short转换成无符号long。
* @since 1.8
*/
public static long toUnsignedLong(short x) {
return ((long) x) & 0xffffL;
}
}