1 阅读顺序
1.4 自个源码笔记(基本上是参照上面两篇进行记述的)
package java.lang;
/* Byte类将一个基本类型byte封装在一个对象中。
一个Byte对象包含了一个byte类型的字段
此外,这个类提供了几种方法,用于将字节转换为String类型,
将String转换为字节,以及处理字节时有用的其他常量和方法。*/
public final class Byte extends Number implements Comparable<Byte> {
/*
MIN_VALUE静态变量表示byte能取的最小值,值为-128,被final修饰说明不可变;
*/
public static final byte MIN_VALUE = -128;
/*
保存了byte类型可以表示的最大常量
*/
public static final byte MAX_VALUE = 127;
/*
TYPE的toString的值是byte。
Class的getPrimitiveClass是一个native方法,在Class.c中有个Java_java_lang_Class_getPrimitiveClass方法与之对应,
所以JVM层面会通过JVM_FindPrimitiveClass函数会根据”byte”字符串获得jclass,最终到Java层则为Class<Byte>。
*/
@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
/**
利用10进制对byte参数进行转换,利用toString()方法进行返回。
*/
public static String toString(byte b) {
return Integer.toString((int)b, 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));
}
}
/*
返回Byte实例,表示特定的byte 值。
利用该方法比用构造方法好多了,省空间和效率较高
*/
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
/**
该方法中有两个参数,第一个参数表示待转换的数字字符串,第二个表示进制数(如二进制,八进制,十进制),
关于字符串转为数值主要是调用Integer.parseInt()方法进行转换。
在获取int值之后,判断是否在byte的范围,如果不在,则抛出异常。
*/
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;
}
public static byte parseByte(String s) throws NumberFormatException {
return parseByte(s, 10);
}
/**
实际上是parseByte()方法进行解析的
*/
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
/**
默认是利用10进制将字符串转成Byte类型。
*/
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
/**
decode方法主要作用是解码字符串转成Byte型,
比如Byte.decode("11")的结果为11,而Byte.decode("0x11")结果为17,因为后面的是十六进制,它会根据实际情况进行解码。
内部调用的是Integer.decode()方法
*/
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;
}
/**
将字符串解析成byte类型
*/
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
// xxxValue方法主要是实现类型提升
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;
}
/**
将值提升位int类型后,再利用Integer的toString()方法转成字符串。
*/
public String toString() {
return Integer.toString((int)value);
}
/**
返回Byte类型的hashCode()值,该值是byte类型提升为int类型。
*/
@Override
public int hashCode() {
return Byte.hashCode(value);
}
/**
直接返回int类型的值。
*/
public static int hashCode(byte value) {
return (int)value;
}
/**
必将是否相同,如果对象参数是Byte的实例且他们的值相等,则返回true。
*/
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();
}
return false;
}
/**
调用的是compare()方法,两byte相减
*/
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
/**
通过相减比较大小,大于0则说明x大于y。
*/
public static int compare(byte x, byte y) {
return x - y;
}
/**
转成无符号的int类型
*/
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
/**
转成无符号的long类型
*/
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}
/**
定义byte的大小,即为8个位,一个字节
*/
public static final int SIZE = 8;
/**
BYTES=1 , 占用一个字节。
*/
public static final int BYTES = SIZE / Byte.SIZE;
/** use serialVersionUID from JDK 1.1. for interoperability */
private static final long serialVersionUID = -7183698231559129828L;
}