目录
类的继承结构图
类的Diagram图
类 & 类的注释
源码分析
全局变量
静态内部类
构造方法
静态方法 —— parseByte()
静态方法 —— valueOf()
静态方法 —— toString()
静态方法 —— hashCode()
静态方法 —— decode()
静态方法 —— toUnsignedInt()
静态方法 —— toUnsignedLong()
静态方法 —— compare()
普通方法 —— equals()
普通方法 —— compareTo()
类型转换方法 —— xxxValue()
下期博文 JDK源码系列 & JAVA语言数据类型Short
类的继承结构图
主要看继承
主要看继承
- 可以看到Byte类继承自Number类。从英文看,知道代表了数字的意思,所以猜测Byte在Java中也是数字的一种展现形式,也可以和其它数字类型进行转换。
类的Diagram图
看继承和实现
看继承和实现
- Comparable接口:说明支持Byte类型对象直接的比较。
- Serializable接口:说明支持序列化和反序列化。
类 & 类的注释
- Byte被final修饰,意味着不能被继承。
源码分析
全局变量
//序列化id
private static final long serialVersionUID = -7183698231559129828L;
//能表达的最小值
public static final byte MIN_VALUE = -128;
//能表达的最大值
public static final byte MAX_VALUE = 127;
//Byte对应的初始类型是byte
//Byte.class和byte.class不相等,但Byte.TYPE和byte.class相等
@SuppressWarnings("unchecked")
public static final Class TYPE = (Class) Class.getPrimitiveClass("byte");
//一个字节二进制表示的位数
public static final int SIZE = 8;
//Byte占用的字节数 1B = 8b
public static final int BYTES = SIZE / Byte.SIZE;
//保存Byte的数据
private final byte value;
-
Byte是byte的包装类,表示一个字节以内的整型数值。范围为-128到127。
-
也就是1Byte = 8b
静态内部类
private static class ByteCache {
//私有构造器,不能被实例化
private ByteCache(){}
//创建长度为256的Byte数组
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
//数组0位置数值为-128,255位置数值为127
cache[i] = new Byte((byte)(i - 128));
}
}
- 该类用于Byte型数值的缓存,提供了一个大小为256的Byte数组,该数组索引从0到255,对应Byte表示的数值范围:-128到127。
构造方法
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
//参数字符串作为十进制解析成byte数组 parseByte()方法后续进行讲解
this.value = parseByte(s, 10);
}
静态方法 —— parseByte()
public static byte parseByte(String s) throws NumberFormatException {
//调用重载方法
return parseByte(s, 10);
}
public static byte parseByte(String s, int radix)
throws NumberFormatException {
//调用Integer的parseInt方法,解析成int型
int i = Integer.parseInt(s, radix);
//判断解析出的数值,是否满足byte型数值的范围
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value out of range. Value:"" + s + "" Radix:" + radix);
//若满足,则进行类型转换,并返回
return (byte)i;
}
静态方法 —— valueOf()
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
//调用parseByte进行转化为byte类型,然后调用重载方法
return valueOf(parseByte(s, radix));
}
public static Byte valueOf(String s) throws NumberFormatException {
//调用重载方法
return valueOf(s, 10);
}
//序列化id
private static final long serialVersionUID = -7183698231559129828L;
//能表达的最小值
public static final byte MIN_VALUE = -128;
//能表达的最大值
public static final byte MAX_VALUE = 127;
//Byte对应的初始类型是byte
//Byte.class和byte.class不相等,但Byte.TYPE和byte.class相等
@SuppressWarnings("unchecked")
public static final Class TYPE = (Class) Class.getPrimitiveClass("byte");
//一个字节二进制表示的位数
public static final int SIZE = 8;
//Byte占用的字节数 1B = 8b
public static final int BYTES = SIZE / Byte.SIZE;
//保存Byte的数据
private final byte value;
-
Byte是byte的包装类,表示一个字节以内的整型数值。范围为-128到127。
-
也就是1Byte = 8b
-
静态内部类
private static class ByteCache {
//私有构造器,不能被实例化
private ByteCache(){}
//创建长度为256的Byte数组
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
//数组0位置数值为-128,255位置数值为127
cache[i] = new Byte((byte)(i - 128));
}
}
- 该类用于Byte型数值的缓存,提供了一个大小为256的Byte数组,该数组索引从0到255,对应Byte表示的数值范围:-128到127。
构造方法
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
//参数字符串作为十进制解析成byte数组 parseByte()方法后续进行讲解
this.value = parseByte(s, 10);
}
静态方法 —— parseByte()
public static byte parseByte(String s) throws NumberFormatException {
//调用重载方法
return parseByte(s, 10);
}
public static byte parseByte(String s, int radix)
throws NumberFormatException {
//调用Integer的parseInt方法,解析成int型
int i = Integer.parseInt(s, radix);
//判断解析出的数值,是否满足byte型数值的范围
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value out of range. Value:"" + s + "" Radix:" + radix);
//若满足,则进行类型转换,并返回
return (byte)i;
}
静态方法 —— valueOf()
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
//调用parseByte进行转化为byte类型,然后调用重载方法
return valueOf(parseByte(s, radix));
}
public static Byte valueOf(String s) throws NumberFormatException {
//调用重载方法
return valueOf(s, 10);
}
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
//参数字符串作为十进制解析成byte数组 parseByte()方法后续进行讲解
this.value = parseByte(s, 10);
}
静态方法 —— parseByte()
public static byte parseByte(String s) throws NumberFormatException {
//调用重载方法
return parseByte(s, 10);
}
public static byte parseByte(String s, int radix)
throws NumberFormatException {
//调用Integer的parseInt方法,解析成int型
int i = Integer.parseInt(s, radix);
//判断解析出的数值,是否满足byte型数值的范围
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value out of range. Value:"" + s + "" Radix:" + radix);
//若满足,则进行类型转换,并返回
return (byte)i;
}
静态方法 —— valueOf()
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
//调用parseByte进行转化为byte类型,然后调用重载方法
return valueOf(parseByte(s, radix));
}
public static Byte valueOf(String s) throws NumberFormatException {
//调用重载方法
return valueOf(s, 10);
}
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
//调用parseByte进行转化为byte类型,然后调用重载方法
return valueOf(parseByte(s, radix));
}
public static Byte valueOf(String s) throws NumberFormatException {
//调用重载方法
return valueOf(s, 10);
}
静态方法 —— toString()
public static String toString(byte b) {
//调用Integer的toString方法
return Integer.toString((int)b, 10);
}
@Override
public String toString() {
return Integer.toString((int)value);
}
静态方法 —— hashCode()
@Override
public int hashCode() {
return Byte.hashCode(value);
}
public static int hashCode(byte value) {
return (int)value;
}
静态方法 —— decode()
public static Byte decode(String nm) throws NumberFormatException {
//调用Integer的decode方法,解析成int
int i = Integer.decode(nm);
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value " + i + " out of range from input " + nm);
//现进行类型转换,再调用valueOf方法,从缓存中获取数值
return valueOf((byte)i);
}
静态方法 —— toUnsignedInt()
public static int toUnsignedInt(byte x) {
//进行类型转换
//oxff为16进制 转为 2进制为 11111111
//走 & 运算
return ((int) x) & 0xff;
}
静态方法 —— toUnsignedLong()
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
静态方法 —— compare()
public static int compare(byte x, byte y) {
return x - y;
}
普通方法 —— equals()
@Override
public boolean equals(Object obj) {
if (obj instanceof Byte) {
//如果是Byte的对象,进行比较里面的数据
return value == ((Byte)obj).byteValue();
}
//否则返回false
return false;
}
普通方法 —— compareTo()
@Override
public int compareTo(Byte anotherByte) {
//调用重载方法
return compare(this.value, anotherByte.value);
}
类型转换方法 —— xxxValue()
@Override
public byte byteValue() {
return value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
下期博文 JDK源码系列 & JAVA语言数据类型Short
@Override
public int hashCode() {
return Byte.hashCode(value);
}
public static int hashCode(byte value) {
return (int)value;
}
静态方法 —— decode()
public static Byte decode(String nm) throws NumberFormatException {
//调用Integer的decode方法,解析成int
int i = Integer.decode(nm);
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value " + i + " out of range from input " + nm);
//现进行类型转换,再调用valueOf方法,从缓存中获取数值
return valueOf((byte)i);
}
静态方法 —— toUnsignedInt()
public static int toUnsignedInt(byte x) {
//进行类型转换
//oxff为16进制 转为 2进制为 11111111
//走 & 运算
return ((int) x) & 0xff;
}
静态方法 —— toUnsignedLong()
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
静态方法 —— compare()
public static int compare(byte x, byte y) {
return x - y;
}
普通方法 —— equals()
@Override
public boolean equals(Object obj) {
if (obj instanceof Byte) {
//如果是Byte的对象,进行比较里面的数据
return value == ((Byte)obj).byteValue();
}
//否则返回false
return false;
}
普通方法 —— compareTo()
@Override
public int compareTo(Byte anotherByte) {
//调用重载方法
return compare(this.value, anotherByte.value);
}
类型转换方法 —— xxxValue()
@Override
public byte byteValue() {
return value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
下期博文 JDK源码系列 & JAVA语言数据类型Short
public static int toUnsignedInt(byte x) {
//进行类型转换
//oxff为16进制 转为 2进制为 11111111
//走 & 运算
return ((int) x) & 0xff;
}
静态方法 —— toUnsignedLong()
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
静态方法 —— compare()
public static int compare(byte x, byte y) {
return x - y;
}
普通方法 —— equals()
@Override
public boolean equals(Object obj) {
if (obj instanceof Byte) {
//如果是Byte的对象,进行比较里面的数据
return value == ((Byte)obj).byteValue();
}
//否则返回false
return false;
}
普通方法 —— compareTo()
@Override
public int compareTo(Byte anotherByte) {
//调用重载方法
return compare(this.value, anotherByte.value);
}
类型转换方法 —— xxxValue()
@Override
public byte byteValue() {
return value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
下期博文 JDK源码系列 & JAVA语言数据类型Short
public static int compare(byte x, byte y) {
return x - y;
}
普通方法 —— equals()
@Override
public boolean equals(Object obj) {
if (obj instanceof Byte) {
//如果是Byte的对象,进行比较里面的数据
return value == ((Byte)obj).byteValue();
}
//否则返回false
return false;
}
普通方法 —— compareTo()
@Override
public int compareTo(Byte anotherByte) {
//调用重载方法
return compare(this.value, anotherByte.value);
}
类型转换方法 —— xxxValue()
@Override
public byte byteValue() {
return value;
}
@Override
public short shortValue() {
return (short)value;
}
@Override
public int intValue() {
return (int)value;
}
@Override
public long longValue() {
return (long)value;
}
@Override
public float floatValue() {
return (float)value;
}
@Override
public double doubleValue() {
return (double)value;
}
下期博文 JDK源码系列 & JAVA语言数据类型Short
@Override
public int compareTo(Byte anotherByte) {
//调用重载方法
return compare(this.value, anotherByte.value);
}



