类
Byte
1.概述
public final class Byte extends Number implements Comparable<Byte>
Byte 类将基本类型 byte 的值包装在一个对象中。一个 Byte 类型的对象只包含一个类型为 byte 的字段。
此外,该类还为 byte 和 String 的相互转换提供了几种方法,并提供了处理 byte 时非常有用的其他一些常量和方法。
2.UML类图
3.源码解析
public final class Byte extends Number implements Comparable<Byte> {
public static final byte MIN_VALUE = -128;
public static final byte MAX_VALUE = 127;
/**
*表示基本类型 byte 的 Class 实例。
*/
@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
/**
*返回表示此 Byte 的值的 String 对象。该值被转换成有符号的十进制表示形式,
*并作为一个 string 返回,正如将 byte 值作为一个参数指定给 toString(byte) 方法所返回的一样。
*/
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
/**
*内部私有静态类
*/
private static class ByteCache {
//私有化构造方法
private ByteCache(){}
//定义一个静态常量Byte数组,并初始化 大小256
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
//静态代码块
static {
//为cache数组没有个元素赋值
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
}
/**
* 返回表示指定 byte 值的一个 Byte 实例。
*/
public static Byte valueOf(byte b) {
final int offset = 128;
// ByteCache.cache[128]=0
return ByteCache.cache[(int)b + offset];
}
/**
* 将 string 参数解析为一个有符号的 byte,其基数由第二个参数指定。
* 基数就是进制数 2<=radix<=36
* 解析出来的数 -128<=i<=127
*/
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;
}
/**
* 将 string 参数解析为有符号的十进制 byte。
*/
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
/**
* 返回一个 Byte 对象,该对象保持从指定的 String 中提取的值,该值是在用第二个参数所给定的基数对指定字符串进行解析时提取的。
*/
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
/**
* 返回一个保持指定 String 所给出的值的 Byte 对象。
*/
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
/**
* 将 String 解码为 Byte。
* decode("33") = valueof("33",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);
}
private final byte value;
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
/**
* 作为一个 byte 返回此 Byte 的值。
*/
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;
}
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;
}
/**
* 通过无符号转换将参数转换为整数。
* @since 1.8
*/
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
/**
* 通过无符号转换将参数转换为long。
* @since 1.8
*/
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
/**
* 用来表示于二进制补码形式的byte值的比特数
* @since 1.5
*/
public static final int SIZE = 8;
/**
*用来表示于二进制补码形式的byte值的字节数
* @since 1.8
*/
public static final int BYTES = SIZE / Byte.SIZE;
/继承的Nubmer实现了java.io.Serializable/
private static final long serialVersionUID = -7183698231559129828L;
}