JDK源码阅读之Short

Short

      Short是基本类型short的包装类,现在我们一起看看它的源码吧!

类图

Short类图

public final class Short extends Number implements Comparable<Short> 

      通过类图和源码我们可以知道Short是不可被继承的,并且Short的对象是可以比较的,由于Short继承了Number(这是一个抽象类),所以Short重写了其所有形如xxxValue的方法。
Number

成员变量

public static final short   MIN_VALUE = -32768;
public static final short   MAX_VALUE = 32767;
public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");//获取Short的class
private final short value;
public static final int SIZE = 16;/表示二进制补码形式的short值的位数,16public static final int BYTES = SIZE / Byte.SIZE;////表示二进制补码形式的short值的字节数,2字节

      Short对象的值保持在value中,并且value是不可变的。

构造方法

public Short(short value) {
	this.value = value;
}
public Short(String s) throws NumberFormatException {
	this.value = parseShort(s, 10);
}
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有两个构造方法,一个是直接接受一个short类型的值即可,将其赋值给value;另一个接受一个字符串类型数据(必须是数字字符串),通过parseShort方法将其转换(十进制),实际上是使用Integer.parseInt将字符串进行转换,如果字符串为null或长度为0(如果首字符为-或+长度要大于1)则抛出NumberFormatException异常,传递的字符串值区间为[-32768,32767],超出此范围抛出NumberFormatException异常信息。

valueOf

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));
	}
}
public static Short valueOf(String s, int radix)
	throws NumberFormatException {
	return valueOf(parseShort(s, radix));
}
public static Short valueOf(String s) throws NumberFormatException {
	return valueOf(s, 10);
}
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);
}

      Short同样能够缓存-128~127之间的数字,并且这个大小是不可调的,而Integer的Cache是可以调整的。如果传递的值在缓存区间则从缓存中取值,否则新创建实例对象。

decode

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

      decode方法将字符串解码为Short,此方法接受十进制字符串,十六进制字符串(用0x\0X#标识),八进制字符串(用0标识),但是解码后的值必须在[-32768,32767]这一区间内,否则抛出NumberFormatException异常。

xxxValue

public byte byteValue() {
    return (byte)value;
}
public short shortValue() {
	return value;
}
public float floatValue() {
	return (float)value;
}
public double doubleValue() {
	return (double)value;
}
public int intValue() {
	return (int)value;
}
public long longValue() {
	return (long)value;
}

      这六个方法都是继承自Number类,将value值强转为对应的类型。

hashCode

public int hashCode() {
	return Short.hashCode(value);
}
public static int hashCode(short value) {
	return (int)value;
}

      Short对象的hash值即为其value值。

equals

首先判断obj是否为Short的实例,再判断value是否相同。

compareTo

public int compareTo(Short anotherShort) {
	return compare(this.value, anotherShort.value);
}
public static int compare(short x, short y) {
	return x - y;
}

      返回大于0前者大于后者,等于0两者相等,小于0前者小于后者。

reverseBytes

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

      将short二进制的高八位和低八位交换。

toUnsignedInt

public static int toUnsignedInt(short x) {
    return ((int) x) & 0xffff;
}

      将short类型的数转换为无符号的int类型。

toUnsignedLong

public static long toUnsignedLong(short x) {
    return ((long) x) & 0xffffL;
}

      将short类型的数转换为无符号的long类型。

文章同步【个人站】

原创文章 234 获赞 1294 访问量 23万+

猜你喜欢

转载自blog.csdn.net/qq_25343557/article/details/100519858