基礎
//final修飾不可更改,每次指派都是建立類(其中-128~127是直接從ByteCache中擷取的不是建立的,可以使用==比較是否相同,其他數值是通過new建立的,不能使用==比較相同,因為位址不同,需用equals比較)
public final class Byte extends Number implements Comparable<Byte> {}
常量
//多少位
public tatic final int SIZE = ;
//位元組數
public static final int BYTES = SIZE / Byte.SIZE;
public static final byte MIN_VALUE = -;
public static final byte MAX_VALUE = ;
繼承
抽象類Number
擷取包裝類與基本類型之間的轉換值,short、int、long、byte、float、double。
實作
Comparable 接口
實作compareTo(T o)方法
數值範圍
最小值:-128
最大值:127
私有靜态内部類
//初始化-128~127的Byte資料
private static class ByteCache {
private ByteCache(){}
static final Byte cache[] = new Byte[-(-) + + ];
static {
for(int i = ; i < cache.length; i++)
cache[i] = new Byte((byte)(i - ));
}
}
方法
byte,Byte轉String
//調用Integer.toString方法預設10進制
public String toString() {}
//調用Integer.toString方法預設10進制
public static String toString(byte b) {}
byte轉Byte
//通過(int)b+128,直接從ByteCache中取值
public static Byte valueOf(byte b) {}
String轉byte,Byte
//s:數字字元串
//radix:字元串的進制(radix不在2~36則抛異常)
//方法先調用Integer.parseInt(),在判斷數字大小,滿足條件進行強制類型轉換
public static byte parseByte(String s, int radix)
throws NumberFormatException {}
//預設字元串為10進制
public static byte parseByte(String s) throws NumberFormatException {}
//調用parseByte,在byte->Byte
public static Byte valueOf(String s, int radix)
throws NumberFormatException {}
//預設字元串為10進制
public static Byte valueOf(String s) throws NumberFormatException {}
//調用Integer.decode(),在判斷數字大小,滿足條件進行強制類型轉換
public static Byte decode(String nm) throws NumberFormatException {}
Byte轉基本資料類型
public byte byteValue() {}
public short shortValue() {}
public int intValue() {}
public long longValue() {}
public float floatValue() {}
public double doubleValue() {}
//無符号x轉int
public static int toUnsignedInt(byte x) {}
//無符号x轉long
public static long toUnsignedLong(byte x) {}
比較
//傳回類型為int,結果為x-y的值。與integer的compare不一樣,integer傳回-1,1,0
public int compareTo(Byte anotherByte) {}
//傳回類型為int,結果為x-y的值。與integer的compare不一樣,integer傳回-1,1,0
public static int compare(byte x, byte y) {}