该类里面的实现方法很简单,其中要注意到的是一个内部类
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
这类在编辑时就已经new好了256个Byte数据了,所以当我们需要用byte时,就要充分利用缓存来获得
看看该方法“:
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
是直接从缓存中拿的
如果是new Byte则
public Byte(byte value) {
this.value = value;
}
是一个新的
Short 跟Byte一样
Integer 与上面两者有多一些方法
转为二进制文本
public static String toBinaryString(int i) {
return toUnsignedString(i, 1);
}
/**
* Convert the integer to an unsigned number.
*/
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0);
return new String(buf, charPos, (32 - charPos));
}
// 转换为16进制(没有前面的0x)
public static String toHexString(int i) {
return toUnsignedString(i, 4);
}
// 转换为8进制
public static String toOctalString(int i) {
return toUnsignedString(i, 3);
}
public static int numberOfLeadingZeros(int i) // 算出前导零
public static int numberOfTrailingZeros(int i) // 算出后导零
public static int numberOfTrailingZeros(int i) // 反转
Long 跟Integer 基本一样