接着上一节讲Number的子类——Byte类。该类继承Number,实现Comparable(Byte)接口。
重点内容
- 构造方法:
public Byte(byte value) {
this.value = value;
}
public Byte(String string) throws NumberFormatException {
this(parseByte(string));
}
- 类方法:
public static final int SIZE = 8;
public static final byte MIN_VALUE = (byte) 0x80;
public static final byte MAX_VALUE = (byte) 0x7F;
@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE
= (Class<Byte>) byte[].class.getComponentType();
public static byte parseByte(String string) throws NumberFormatException {
return parseByte(string, 10);
}
public static byte parseByte(String string, int radix) throws NumberFormatException {
int intValue = Integer.parseInt(string, radix);
byte result = (byte) intValue;
if (result == intValue) {
return result;
}
throw new NumberFormatException("Value out of range for byte: \"" + string + "\"");
public static int compare(byte lhs, byte rhs) {
return lhs > rhs ? 1 : (lhs < rhs ? -1 : 0);
}
public static String toHexString(byte b, boolean upperCase) {
return IntegralToString.byteToHexString(b, upperCase);
}
public static String toString(byte value) {
return Integer.toString(value);
}
public static Byte valueOf(String string) throws NumberFormatException {
return valueOf(parseByte(string));
}
public static Byte valueOf(String string, int radix) throws NumberFormatException {
return valueOf(parseByte(string, radix));
}
private static final Byte[] VALUES = new Byte[256];
static {
for (int i = -128; i < 128; i++) {
VALUES[i + 128] = new Byte((byte) i);
}
}
public static Byte valueOf(byte b) {
return VALUES[b + 128];
}
- 对象方法:
private final byte value;
@Override
public short shortValue() {
return value;
}
@Override
public String toString() {
return Integer.toString(value);
}
@Override
@FindBugsSuppressWarnings("RC_REF_COMPARISON")
public boolean equals(Object object) {
return (object == this) || ((object instance of Byte) && (((Byte) object).value == value));
}
@Override
public float floatValue() {
return value;
}
@Override
public int hashCode() {
return value;
}
@Override
public byte byteValue() {
return value;
}
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
@Override
public double doubleValue() {
return value;
}
public int compareTo(Byte object) {
return compare(value, object.value);
}