源码分析,基本上都加载注解上了,如有谬误,请指正,谢谢。
jdk1.8.0_161
public class Byte extends Number implements Serializable {
/**
* Byte包含的基础byte的最小值,也就是-2的7次方
*/
public static final byte MIN_VALUE = -128;
/**
* Byte包含的基础byte的最大值,也即2的7次方减1
*/
public static final byte MAX_VALUE = 127;
/**
* Byte的class对象
*/
@SuppressWarnings("unchecked")
// public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
/**
* toString,静态
*/
public static String toString(byte b) {
return Integer.toString((int) b, 10);
}
/**
* Byte对象的缓存类
*/
private static class ByteCache {
private ByteCache() {
}
/**
* 缓存数组
*/
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
static {
// 缓存初始化,范围是-128到127
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;
// 因为数组角标是从0开始的,所以这里要做一个转换
return ByteCache.cache[(int) b + offset];
}
/**
* 将字符串根据指定的基数格式化成基础byte,字符串可以包含数组和字母,而基数最大是36
*/
public static byte parseByte(String s, int radix)
throws NumberFormatException {
// 这里其实调用了Integer的格式化。
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;
}
/**
* 将字符串根据10进制转换成基础byte
*/
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
/**
* 将字符串根据指定的基数包装成Byte对象
*/
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);
}
/**
* 这个是将2进制或者是8进制或者是16进制的字符串转成封装成Byte对象
*/
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);
}
/**
* Byte对象中具体的byte值,final
*/
private final byte value;
/**
* 初始化
*/
public Byte(byte value) {
this.value = value;
}
/**
* 初始化,字符串形式
*/
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
/**
* 获取Byte对象的byte值,继承自Number方法
*/
public byte byteValue() {
return value;
}
/**
* 获取Byte对象的short值,继承自Number方法
*/
public short shortValue() {
return (short) value;
}
/**
* 获取Byte对象的int值,继承自Number方法
*/
public int intValue() {
return (int) value;
}
/**
* 获取Byte对象的long值,继承自Number方法
*/
public long longValue() {
return (long) value;
}
/**
* 获取Byte对象的float值,继承自Number方法
*/
public float floatValue() {
return (float) value;
}
/**
* 获取Byte对象的double值,继承自Number方法
*/
public double doubleValue() {
return (double) value;
}
/**
* toString
*/
public String toString() {
// 这里是先把byte升级为int,然后用Integer的toString方法
return Integer.toString((int) value);
}
/**
* hashCode
*/
@Override
public int hashCode() {
return Byte.hashCode(value);
}
/**
* hashCode,静态,byte的hashCode就是她的值
*/
public static int hashCode(byte value) {
return (int) value;
}
/**
* equals
*/
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte) obj).byteValue();
}
return false;
}
/**
* 比较本对象个指定Byte
*/
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
/**
* 比较两个byte
*/
public static int compare(byte x, byte y) {
return x - y;
}
/**
* 将指定byte专为无符号int
*/
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
/**
* 将指定byte专为无符号long
*/
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
/**
* 用于表示二进制补码二进制形式的字节值的位数
*/
public static final int SIZE = 8;
/**
* 用于表示二进制补码二进制形式的字节值的字节数
*/
public static final int BYTES = SIZE / Byte.SIZE;
/**
* 序列化
*/
private static final long serialVersionUID = -7183698231559129828L;
}
网友评论