Short范围是包含在Integer的范围之内的,其中大部分的方法是一样的,也是调用Integer中的方法进行处理的,没有什么难度!!
package java.lang;
public final class Short extends Number implements Comparable<Short> {
// Short 的最大值与最小值
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类型的数据转成String
// 默认调用Integer.toString(), 不传基数,默认基数为10
public static String toString(short s) {
return Integer.toString((int)s, 10);
}
// 将数字字符串转化成Short数据,index为数字字符串s的基数
public static short parseShort(String s, int radix)
throws NumberFormatException {
// 调用Integer.parseInt(),获取到返回的int数据后对其范围进行判断,是否在short范围之外
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数据,不传入基数,默认基数为10
public static short parseShort(String s) throws NumberFormatException {
return parseShort(s, 10);
}
// 将数字字符串转化成Short数据,index为数字字符串s的基数
public static Short valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseShort(s, radix));
}
// 将数字字符串转化成Short数据,不传入基数,默认基数为10
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];
static {
// 初始化时将[-128, 127]缓存到cache[]中
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
// 获取到short数据,自动装箱时判断是否直接取数据还是新new对象
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
// 判断范围是否在[-128, 127]之间,是就取缓存,不是就新new对象
if (sAsInt >= -128 && sAsInt <= 127) {
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
// 解码,将其它进制的nm字符串,转换成10进制的short数据
public static Short decode(String nm) throws NumberFormatException {
// 实际调用的是Integer.decode()方法
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数据转成其它的数据类型
public Short(short value) {
this.value = value;
}
public Short(String s) throws NumberFormatException {
this.value = parseShort(s, 10);
}
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;
}
// 将short转换成字符串
public String toString() {
return Integer.toString((int)value);
}
// 获取short变量撤销hashCode值
@Override
public int hashCode() {
return Short.hashCode(value);
}
// 计算short类型的hashCode值,
public static int hashCode(short value) {
return (int)value;
}
// 比较两个short数据是否一样,参数必须是Short类型数据
public boolean equals(Object obj) {
if (obj instanceof Short) {
return value == ((Short)obj).shortValue();
}
return false;
}
// 比较两个short数据,返回两个short的差值
public int compareTo(Short anotherShort) {
return compare(this.value, anotherShort.value);
}
// 比较两个short数据,返回两个short的差值
public static int compare(short x, short y) {
return x - y;
}
// short的最大长度
public static final int SIZE = 16;
//
public static final int BYTES = SIZE / Byte.SIZE;
// 反转
public static short reverseBytes(short i) {
return (short) (((i & 0xFF00) >> 8) | (i << 8));
}
// 转换成无符号的int数据
public static int toUnsignedInt(short x) {
return ((int) x) & 0xffff;
}
// 转换成无符号的int数据
public static long toUnsignedLong(short x) {
return ((long) x) & 0xffffL;
}
private static final long serialVersionUID = 7515723908773894738L;
}