package java.lang
文章目录
- @since JDK1.1
public final class Byte extends Number implements Comparable< Byte> {
ByteCache缓冲区
通过valueOf()方法返回的Byte对象都是同一个对象
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));
}
}
成员变量
byte的上下区间MAX_VALUE、MIN_VALUE
public static final byte MIN_VALUE = -128
public static final byte MAX_VALUE = 127
private final byte value
构造方法
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
成员方法
valueOf
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
parseByte和valueOf
1,将String转换为Byte,默认十进制
2,valueOf是通过调用parseByte()实现函数功能
public static byte parseByte(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 (byte)i;
}
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
decode
public static Byte 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((byte)i);
}
[外链图片转存失败(img-UbJwCsYn-1566206461853)(…/…/img/ByteEncodeAndValueOF.png)]
[外链图片转存失败(img-47cmIvFH-1566206461860)(…/…/img/ByteEncodeAndValueOFResult.png)]
- decode()parseByte()都是用于将String转换为Byte,区别在于decode可以解析八进制、十进制、十六进制的String,而valueOf只能解析数字字面量
- decode通过调用valueOf()实现函数功能,关键在于第一步的String转换为int,感兴趣的可以翻看我的Integer源码解析文章//TODO
获取Value
public byte byteValue() {
return value;
}
public short shortValue() {
return (short)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;
}
toString()
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
public String toString() {
return Integer.toString((int)value);
}
hashCode()和equals()
@Override
public int hashCode() {
return Byte.hashCode(value);
}
public static int hashCode(byte value) {
return (int)value;
}
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();
}
return false;
}
compareTo
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
public static int compare(byte x, byte y) {
return x - y;
}
toUnsignedInt()和toUnsignedInt()
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
}