上節我們介紹過JDK源碼-Boolean類
本節我們介紹Byte類,Byte類主要的作用就是對基本類型byte進行封裝,提供了一些處理byte類型的方法,比如byte到String類型的轉換方法或String類型到byte類型的轉換方法。
java的基本數據類型byte、short、int、long、float、double、char所佔的字節長度分別爲1、2、4、8、4、8、2。byte即字節的意思,1個字節=8bit,以二進制補碼錶示的整數,值域被定義爲-128~127,通常在讀取文件時需要用字節數組來保存文件內容。byte數組也常被用作緩衝器,接收文件內容。
一、實現接口
Byte類繼承了Number類,並且實現了Comparable接口,
public final class Byte extends Number implements Comparable<Byte>
二、值域
byte爲1個字節,即8位,最高位是符號位,byte的最大正數就是 01111111(最高位必須是0),也就是 127。最小值10000000,取反:01111111 加1得到 10000000 最後得到 -128。被final修飾說明不可變;
public static final byte MIN_VALUE = -128;
public static final byte MAX_VALUE = 127;
爲什麼不是 -128 到 128呢?
運算規則:
正數的最高位都是 0 ,正數的值:二進制表示的值。
負數的最高位都是 1 ,負數的值:取反後加1 然後加個負號得到得值。
8位的二進制來說明一下此規則:比如:00000001。最高位是0 爲正數 ,那麼表示的就是 十進制的 1。
再比如:10000001.最高位是1 爲負數,值是多少?取反得到 01111110 加1 得到 01111111 ,那麼值爲 -127
三、構造方法
包含兩種構造函數,分別可以傳入byte和String類型,通過傳byte數據來構造對象和傳字符串數據來創建對象,它是通過調用parseByte方法進行轉換的,轉換邏輯見下面的parseByte(s, 10)方法。
public Byte(byte value) {
this.value = value;
}
public Byte(String s) throws NumberFormatException {
this.value = parseByte(s, 10);
}
用來存放Byte對象那byte對應的值。
private final byte value;
用來表示於二進制補碼形式的byte值的位數,值爲8
public static final int SIZE = 8;
用來表示於二進制補碼形式的byte值的字節數,值爲1
public static final int BYTES = SIZE / Byte.SIZE;
四、常用方法
toString(byte b)
內部調用了Integer.toString(int i, int radix)方法返回String類型字符串
public static String toString(byte b) {
return Integer.toString((int)b, 10);
}
ByteCache內部類
ByteCache是Byte的一個內部類,它包含了byte所有可能值的Byte數組,對於byte來說它的可能值就是從-128到127,一共256個,所以我們只需要實例化256個Byte對象就可以表示所有可能的byte。在靜態代碼塊中創建了這256個對象放到cache數組中,靜態代碼塊在類一加載的時候就會執行,而且這些都是靜態且final的,避免重複的實例化和回收。
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));
}
}
valueOf(byte b)
因爲ByteCache包含了所有byte可能值的Byte對象,直接從ByteCache的數組中獲取對應的Byte對象即可。數組下標是從0開始的不包含負數,因此加上一個offset=128來取得數組對應位置的元素值
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
}
valueOf(String s, int radix)
此方法先調用parseByte(s, radix)將字符串轉換爲byte,然後調用valueOf(byte b)取得數組對應位置的元素值
public static Byte valueOf(String s, int radix) throws NumberFormatException {
return valueOf(parseByte(s, radix));
}
//此方法就是默認進製爲十進制
public static Byte valueOf(String s) throws NumberFormatException {
return valueOf(s, 10);
}
parseByte(String s, int radix)
此方法是將一個字符串轉換爲byte,第一個參數是待轉換的字符串,第二個參數表示進制數,這裏的轉換其實是調了Integer的parseInt方法,返回值再判斷是不是在byte的最小值和最大值之間。
例如:Byte.parseByte(“100”,10)表示十進制的100,所以值爲100,而Byte.parseByte(“100”,2)表示二進制的100,所以值爲4。
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);
}
decode(String nm)
此方法主要作用是解碼字符串轉成Byte型,比如Byte.decode(“11”)的結果爲11,而Byte.decode(“0x11”)結果爲17,因爲後面的是十六進制,它會根據實際情況進行解碼。最後判斷下轉換後的數據是否在byte類型的數值範圍內,然後調用valueOf(byte b)方法取得數組對應位置的元素值。
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);
}
xxxValue方法
包括shortValue、intValue、longValue、floatValue和doubleValue等方法,重寫了父類Number類中的xxxValue方法,其實就是java的強制類型轉換機制。
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;
}
hashCode方法
hashCode方法就是直接返回int類型的值。
@Override
public int hashCode() {
return Byte.hashCode(value);
}
public static int hashCode(byte value) {
return (int)value;
}
equals(Object obj)
比較是否相同時先判斷是不是Byte類型再比較值。
public boolean equals(Object obj) {
if (obj instanceof Byte) {
return value == ((Byte)obj).byteValue();
}
return false;
}
compareTo方法
實現了Comparable接口中的抽象方法,內部調用了compare(byte x, byte y)方法,通過相減來比較,大於0則說明x大於y。
public int compareTo(Byte anotherByte) {
return compare(this.value, anotherByte.value);
}
public static int compare(byte x, byte y) {
return x - y;
}
無符號轉換
轉成無符號int型和無符號long型。
public static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
public static long toUnsignedLong(byte x) {
return ((long) x) & 0xffL;
}