探索JDK之Short类
1. 简介
Short类封装了一个值short原始类型的一个对象。一个Short类型的对象包含一个字段的类型是short。此外,该类提供了一些方法处理short、String的相关的操作。
2. 源码
Short类中属性有:
- 实例变量
- 实例方法
- 类变量
- 类方法
- 静态内部类
- 构造器
其他
(1) 实例变量:
1.value:表示数值大小(final不可变)
private final short value;
(2) 实例方法:
1.byteValue:转换为byte
public byte byteValue() {
return (byte)value;
}
2.shortValue:转换为short,直接返回value
public short shortValue() {
return value;
}
3.intValue:转换为int
public int intValue() {
return (int)value;
}
4.longValue:转换为long
public long longValue() {
return (long)value;
}
5.floatValue:转换为float
public float floatValue() {
return (float)value;
}
6.doubleValue:转换为double
public double doubleValue() {
return (double)value;
}
7.toString:重写toString方法,先转换为int,调用Integer中方法toString
public String toString() {
return Integer.toString((int)value);
}
8.hashCode:重写hashCode方法,调用静态hashCode,实际就是返回value值
@Override
public int hashCode() {
return Short.hashCode(value);
}
9.equals:判断当前Short与另一对象的value是否相等
public boolean equals(Object obj) {
if (obj instanceof Short) {
return value == ((Short)obj).shortValue();
}
return false;
}
10.compareTo:返回两个short的差值,x-y
public int compareTo(Short anotherShort) {
return compare(this.value, anotherShort.value);
}
(3) 类变量:
1.MIN_VALUE:short最小值,-32768
public static final short MIN_VALUE = -32768;
2.MAX_VALUE:short最大值,32767
public static final short MAX_VALUE = 32767;
3.TYPE:对应的原始类的类型short
public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short");
4.SIZE:short类型数值二进制占用位数,为16
public static final int SIZE = 16;
5.BYTES:short类型数值二进制占用字节数,为2
public static final int BYTES = SIZE / Byte.SIZE;
6.serialVersionUID:序列版本号
private static final long serialVersionUID = 7515723908773894738L;
(4) 类方法:
1.toString(short):重写toString方法,返回value
直接调用了Integer.parseInt(String, int)方法
public static String toString(short s) {
return Integer.toString((int)s, 10);
}
2.parseShort(String, int):解析String为short,base为radix,(不是每次new,看缓存池是否存在)
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;
}
3.parseShort(String):解析String为short,base为10,(不是每次new,看缓存池是否存在)
public static short parseShort(String s) throws NumberFormatException {
return parseShort(s, 10);
}
4.valueOf(String, int):同parseShort(String, int)
public static Short valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseShort(s, radix));
}
5.valueOf(String):同parseShort(String)
public static Short valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
6.valueOf(short):返回short值的Short对象,(不是每次new,看缓存池是否存在)
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);
}
7.decode(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);
}
8.hashCode():重写hashCode,返回value值
public static int hashCode(short value) {
return (int)value;
}
9.compare(short, short):返回x-y的差值,返回类型为int
public static int compare(short x, short y) {
return x - y;
}
10.reverseBytes(short):二进制按位反转
public static short reverseBytes(short i) {
return (short) (((i & 0xFF00) >> 8) | (i << 8));
}
11.toUnsignedInt(short)::二进制按无符号类型重写计算值,返回int类型
public static int toUnsignedInt(short x) {
return ((int) x) & 0xffff;
}
12.toUnsignedLong(short)::二进制按无符号类型重写计算值,返回long类型
public static long toUnsignedLong(short x) {
return ((long) x) & 0xffffL;
}
(5) 构造器:
1.Short(short):
public Short(short value) {
this.value = value;
}
2.Short(String):
public Short(String s) throws NumberFormatException {
this.value = parseShort(s, 10);
}
(6) 静态内部类:
1.ShortCache:Short对象缓存池,缓存范围-128-127
与IntegerCache不同的是,ShortCache大小和范围是固定的
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));
}
}
本人也是在慢慢学习中,如有错误还请原谅、敬请指出,谢谢!