Byte源碼分析
一.概述
Byte是byte的包裝類,表示一個字節以內的整型數值。範圍爲-128到127。
Byte.java中的相關代碼:
public final class Byte extends Number implements Comparable<Byte> {
…
}
1.Byte被final修飾,不能被繼承。
2.繼承了Number,可以實現數值間的轉換。
3.實現了Comparable<Byte>接口,可以進行Byte類型對象之間的比較。
二.源碼分析
1.全局變量
Byte.java中的相關代碼:
public static final byte MIN_VALUE = -128; // byte能表示的最小值
public static final byte MAX_VALUE = 127; // byte能表示的最大值
// 抑制沒有類型檢查而出現的警告
@SuppressWarnings("unchecked")
// Byte類中對應的原始的byte
// Byte.class和byte.class不相等,但Byte.TYPE和byte.class相等
public static final Class<Byte> TYPE = (Class<Byte>)Class.getPrimitiveClass("byte");
private final byte value; // 用於保存Byte的值
public static final int SIZE = 8; // 表示一個字節二進制表示的位數
public static final int BYTES = SIZE / Byte.SIZE; // 表示Byte佔的字節數
private static final long serialVersionUID = -7183698231559129828L; // 用於序列化
2.靜態內部類ByteCache
該類用於Byte型數值的緩存,提供了一個大小爲256的Byte數組,該數組索引從0到255,對應Byte表示的數值範圍:-128到127。
Byte.java中的相關代碼:
private static class ByteCache {
// 私有化默認的構造方法,因此不能創建該類的對象實例
private ByteCache(){}
// 創建長度爲256的Byte數組
// 128表示128個負數,127爲127個正數,1表示0
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
// 靜態初始化代碼塊
static {
// 循環填充數據
for(int i = 0; i < cache.length; i++)
// // 128爲偏置,數組0位置數值爲-128,255位置數值爲127
cache[i] = new Byte((byte)(i - 128));
}
}
3.構造方法
1)參數爲byte
Byte.java中的相關代碼:
public Byte(byte value) {
// 保存數值到全局變量
this.value = value;
}
2)參數爲String
Byte.java中的相關代碼:
public Byte(String s) throws NumberFormatException {
// 調用parseByte方法,作爲十進制數解析成byte型數值
this.value = parseByte(s, 10);
}
4. parseByte方法
將帶有+、-號的字符串按十進制解析成byte型數值。
Byte.java中的相關代碼:
public static byte parseByte(String s) throws NumberFormatException {
// 調用重載方法
return parseByte(s, 10);
}
調用重載的parseByte方法。
指定進制對字符串進行解析。
Byte.java中的相關代碼:
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;
}
5. valueOf方法
將String類型的對象轉換爲Byte型。
Byte.java中的相關代碼:
public static Byte valueOf(String s) throws NumberFormatException {
// 調用重載方法,並返回
return valueOf(s, 10);
}
調用了重載的valueOf方法。
指定進制對String對象進行轉換。
Byte.java中的相關代碼:
public static Byte valueOf(String s, int radix)
throws NumberFormatException {
// 調用parseByte方法進行轉換
// 轉換的結果作爲參數,調用重載方法,並返回
return valueOf(parseByte(s, radix));
}
調用了重載的valueOf方法。
Byte.java中的相關代碼:
public static Byte valueOf(byte b) {
// 128爲偏置
final int offset = 128;
// 從緩存中獲取
return ByteCache.cache[(int)b + offset];
}
6. decode方法
對字符串形式的數值進行解析。
decode方法支持的字符串形式比parseByte方法更豐富。
支持不同進制的標識符(0x、#等)和小數形式等。
Byte.java中的相關代碼:
public static Byte decode(String nm) throws NumberFormatException {
// 調用Integer的decode方法,解析成int型
int i = Integer.decode(nm);
// 判斷解析出的數據是否滿足byte型數值的範圍要求
if (i < MIN_VALUE || i > MAX_VALUE)
throw new NumberFormatException(
"Value " + i + " out of range from input " + nm);
// 現進行類型轉換,再調用valueOf方法,從緩存中獲取數值
return valueOf((byte)i);
}
7. byteValue方法
獲取當前byte類型的數值
Byte.java中的相關代碼:
public byte byteValue() {
// 直接返回
return value;
}
8. shortValue方法
將當前byte型的數值轉換爲short類型的數值。
Byte.java中的相關代碼:
public short shortValue() {
// 進行類型轉換,並返回
return (short)value;
}
9. intValue方法
將當前byte型的數值轉換爲int類型的數值。
Byte.java中的相關代碼:
public int intValue() {
// 進行類型轉換,並返回
return (int)value;
}
10. longValue方法
將當前byte型的數值轉換爲long類型的數值。
Byte.java中的相關代碼:
public long longValue() {
// 進行類型轉換,並返回
return (long)value;
}
11. floatValue方法
將當前byte型的數值轉換爲float類型的數值。
Byte.java中的相關代碼:
public float floatValue() {
// 進行類型轉換,並返回
return (float)value;
}
12. doubleValue方法
將當前byte型的數值轉換爲double類型的數值。
Byte.java中的相關代碼:
public double doubleValue() {
// 進行類型轉換,並返回
return (double)value;
}
13.toString方法
將byte型數值轉換爲String型。
1)無參數
Byte.java中的相關代碼:
public String toString() {
// 調用Integer的toString方法進行轉換
return Integer.toString((int)value);
}
2)參數爲byte
Byte.java中的相關代碼:
public static String toString(byte b) {
// 調用Integer的toString方法,按十進制的方式進行轉換
return Integer.toString((int)b, 10);
}
14. hashCode方法
求byte數值的哈希值。
Byte.java中的相關代碼:
@Override
public int hashCode() {
// 調用Byte的靜態方法hashCode
return Byte.hashCode(value);
}
1)hashCode方法
Byte.java中的相關代碼:
public static int hashCode(byte value) {
// 類型轉換並返回
return (int)value;
}
Byte型對象的哈希值就是它本身的值。
15. equals方法
比較兩個Byte對象是否相等。
Byte.java中的相關代碼:
public boolean equals(Object obj) {
// 若對象類型爲Byte
if (obj instanceof Byte) {
// 比較兩者是否相等
return value == ((Byte)obj).byteValue();
}
// 若類型不爲Byte,返回false
return false;
}
16. compareTo方法
比較兩個Byte對象是否相等。
若相等則返回0,若不相等則返回兩個數之差。
Byte.java中的相關代碼:
public int compareTo(Byte anotherByte) {
// 調用compare方法
return compare(this.value, anotherByte.value);
}
1)compare方法。
Byte.java中的相關代碼:
public static int compare(byte x, byte y) {
// 返回兩個數之差
return x - y;
}
17. toUnsignedInt方法
將當前byte型的數值轉換爲無符號int型數值。
Byte.java中的相關代碼:
public static int toUnsignedInt(byte x) {
// 進行類型轉換,和11111111B進行與運算,返回
return ((int) x) & 0xff;
}
18. toUnsignedLong方法
將當前byte型的數值轉換爲無符號long型數值。
Byte.java中的相關代碼:
public static long toUnsignedLong(byte x) {
// 進行類型轉換,和11111111B進行與運算,返回
return ((long) x) & 0xffL;
}