一、结构图
二、Number抽象类介绍。
抽象类Number是平台类的超类,表示可转换为基本类型byte 、 double 、 float 、 int 、 long和short数值。 它更像是一种规范,规定了继承这个抽象类的子类可以互相转换。
1、结构
2、方法介绍
// 将数据转换为int类型
public abstract int intValue();
// 将数据转换为long类型
public abstract long longValue();
// 将数据转换为float类型
public abstract float floatValue();
// 将数据转换为double类型
public abstract double doubleValue();
// 将数据转换为byte类型
public byte byteValue() {
return (byte)intValue();
}
// 将数据转换为short类型
public short shortValue() {
return (short)intValue();
}
这些类型转换中非浮点数类型可能涉及到截断取舍,浮点数类型可能涉及到四舍五入。
三、Byte类介绍
Byte类将原始类型byte的值包装在一个对象中。
1、结构
2、静态属性方法介绍
// 最小8bit整数
public static final byte MIN_VALUE = -128;
// 最大8bit整数
public static final byte MAX_VALUE = 127;
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
// 非静态属性,用来存储byte
private final byte value;
// 用于以二进制补码形式表示byte值的位数。
public static final int SIZE = 8;
// 用于以二进制补码形式表示byte值的byte数。就是1。
public static final int BYTES = SIZE / Byte.SIZE;
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
将byte转为字符串,10代表转为几进制。
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));
}
}
内部类,存储value为-127-128的Byte对象。
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
返回缓存对象(注意加上偏移量)。
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;
}
将radix进制的String类型的整数转换为byte类型。
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
默认10进制
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
转换为Byte类型
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
默认10进制
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);
}
Integer.decode将String类型的字符串解码为10进制的Integer类型。
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
将byte转换为无符号的int类型,ff的二进制是11111111,首先将x强转为int类型,再进行&运算,这里无论正数byte还是负数byte都会转换为低7位的正数。
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
同上。
3、方法介绍
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
构造方法。
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;
}
继承Number抽象类要实现的内容,因为Byte只有一个字节,所以它和其他数值类型转换都是向上转型。
public String toString() {
return Integer.toString((int)value);
}
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;
}
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
public static int compare(byte x, byte y) {
return x - y;
}
不解释。