short是java基本数据类型中的一种,为短整型,占2个字节,16位。Short为short类型的包装类。
基础知识:
1.整型变量的分类,java中整型变量分为以下四类:
短整型,类型关键字为short[int]占2个字节。
整型,类型关键字为int占4个字节。
长整型,类型关键字为long[int]占8个字节。
无符号整型,类型关键字为unsigned[int]或unsignedshort或unsignedlong
Short类源码:
public final class Short extends Number implements Comparable<Short> {
//short类型最小值
public static final short MIN_VALUE = -32768;
//short类型最大值
public static final short MAX_VALUE = 32767;
//基本类型 short 的 Class 实例,即short.class
public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short");
//将short类型的数转换为其10进制对应的数的字符串形式
public static String toString(short s) {
return Integer.toString((int)s, 10);
}
//将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;
}
//将10进制的字符串数转换为对应的short类型的数字
public static short parseShort(String s) throws NumberFormatException {
return parseShort(s, 10);
}
//将指定进制的字符串数转换为对应的short类型的数字
public static Short valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseShort(s, radix));
}
//获取字符串形式的数字,所对应的Short对象
public static Short valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
//Short对象的缓冲类
private static class ShortCache {
private ShortCache(){}
//缓冲数组
static final Short cache[] = new Short[-(-128) + 127 + 1];
//创建缓冲对象,共256个
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
//从缓冲对象中获取对应的Short对象
//可以看到在-127到128之间是从缓存数组中取的,其它范围的是创建的新的对象
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);
}
//将其它进制表示法,表示的字符串数据转换为10进制表示的short数据,然后返回对应的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);
}
//用于存储short数值
private final short value;
//Short类的构造器,参数为short值
public Short(short value) {
this.value = value;
}
//Short类的构造器,参数为字符串
public Short(String s) throws NumberFormatException {
this.value = parseShort(s, 10);
}
//将short数转换为byte,由于short长度大于byte所以这里可以会出现溢出的情况
public byte byteValue() {
return (byte)value;
}
//返回short数据
public short shortValue() {
return value;
}
//将short数转换为int类型
public int intValue() {
return (int)value;
}
//将short数转换为long类型
public long longValue() {
return (long)value;
}
//将short数转换为float类型
public float floatValue() {
return (float)value;
}
//将short数转换为double类型
public double doubleValue() {
return (double)value;
}
//toString方法
public String toString() {
return Integer.toString((int)value);
}
public int hashCode() {
return Short.hashCode(value);
}
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对象的大小
public int compareTo(Short anotherShort) {
return compare(this.value, anotherShort.value);
}
public static int compare(short x, short y) {
return x - y;
}
//short类型所占的位数
public static final int SIZE = 16;
//short类型所占的字节数
public static final int BYTES = SIZE / Byte.SIZE;
//short的高8位与低8位互换
public static short reverseBytes(short i) {
return (short) (((i & 0xFF00) >> 8) | (i << 8));
}
//将short类型的数转换为无符号的int类型
public static int toUnsignedInt(short x) {
return ((int) x) & 0xffff;
}
//将short类型的数转换为无符号的long类型
public static long toUnsignedLong(short x) {
return ((long) x) & 0xffffL;
}
}
可以看到在Short类的源码中绝大部分属性或方法都跟Byte类类似,这里我就不再重复讲解了,不明白的同学可以看下Byte类的源码学习。
与Byte类的不同:
1.缓存池:
private static class ShortCache {
private ShortCache(){}
//缓冲数组
static final Short cache[] = new Short[-(-128) + 127 + 1];
//创建缓冲对象,共256个
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
可以看到Short类的缓存池中也是存放了256个对象,那为什么说这里与Byte类不同呢,原因很简单,因为short类型的范围是-32768到32767,这里只缓存了256个对象,那为什么不全部缓存下来呢,因为太多了,占用的内在空间较大,所以在使用Short类的时候,对于-128到127内的Short对象是从缓存池中取的,其它的是创建的新对象。
2.reverseBytes方法
public static short reverseBytes(short i) {
return (short) (((i & 0xFF00) >> 8) | (i << 8));
}
可以看到在Byte类中没有这个方法,这个方法是用来将short类型的低8位与高8位互换的。解释如下:
1.将目标数i与0xFF00(转换为2进制是 1111 1111 0000 0000)做&的运算,得到的结果为i的高8位不变,低8位全变为0,然后右移8位,就得到了目标数i的高8位
2.将i左移8位,得到的数为一个新数,这个新数的高8位为原目标数i的低8位,这个数的低8位为0,然后将i的高8位与这个新数做|运算,得到的结果就是将目标数i的高8位,与低8位互换的数。
有兴趣的同学可以亲自先一个数运算一下,如果不清楚位运算&和|可以看下一起学JDK源码 -- Byte类的基础知识。
网友评论