JDK源码分析:Byte.java

  Byte是基本数据类型byte的包装类。

  1)声明部分:

public final class Byte extends Number implements Comparable<Byte> 

  实现Comparable<T>接口,实现该接口方法如下:

public int compareTo(Byte anotherByte) {  
    return compare(this.value, anotherByte.value);  
}  
public static int compare(byte x, byte y) {  
    return x - y;  
} 

  继承Number.java,方法如下:

public abstract int intValue();  
public abstract float floatValue();  
public abstract long longValue();  
public abstract double doubleValue();  
public byte byteValue() {  
    return (byte)intValue();  
}  
public short shortValue() {  
    return (short)intValue();  
}

  其中前4个是抽象方法,Byte.java不是抽象类,所以必须实现父类的4个抽象方法;后2个方法实现调用第一个方法。Number提供了包装类型之间的强类型转换(JDK目前已经实现了自动拆箱和装箱操作)。

   2)属性部分

//min value
public static final byte MIN_VALUE = -128;
//max value public static final byte MAX_VALUE = 127; //The {@code Class} instance representing the primitive type public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte"); //The value of the {@code Byte}. private final byte value; //The number of bits used to represent a {@code byte} value in two's complement binary form. public static final int SIZE = 8; //The number of bytes used to represent a {@code byte} value in two's complement binary form. public static final int BYTES = SIZE / Byte.SIZE;

  3)私有内部静态类

//Byte Cache -128~127  
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));  
    }  
}

  含有静态模块,class加载的时候,执行静态模块,初始化cache[]。

  4)Byte的声明

  构造方法重载:

//构造方法重载1  
public Byte(byte value) {  
    this.value = value;  
}  
//构造方法重载2  
public Byte(String s) throws NumberFormatException {  
    this.value = parseByte(s, 10);  
} 

  类型转换为Byte的方法,除了构造方法和自动装箱外: 

public static Byte valueOf(byte b) {  
    final int offset = 128;  
    return ByteCache.cache[(int)b + offset];  
}  
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);  
}  
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);  
}  

  观察这几个方法,public static Byte valueOf(byte b)和public static byte parseByte(String s, int radix)是核心。第2个方法转换为byte;第一个方法转换为Byte,Byte根据byte的值,从缓存中获取Byte对象。

  例子:

//构造方法重载  
byte byte1 = 1;  
Byte b1 = new Byte(byte1);  
Byte b2 = new Byte("1");  
Byte b3 = byte1;//自动装箱  
out(b1==b2);//fasle  
out(b1==b3);//fasle  
//初始化方法,除构造方法和自动装箱  
Byte b4 = Byte.parseByte("1",10);//最终byte自动装箱  
Byte b5 = Byte.parseByte("1");//同上  
Byte b6 = Byte.valueOf("1",10);//最终取ByteCache  
Byte b7 = Byte.valueOf("1");//同上  
Byte b8 = Byte.valueOf(byte1);//同上  
out(b4==b2);//fasle  
out(b4==b1);//fasle  
out(b4==b3);//true  
out(b4==b5);//true  
out(b6==b7);//true  
out(b6==b8);//true  

  从例子可以得出结论:采用自动装箱会默认根据byte值去获取ByteCache,而使用构造方法重载不会去获取ByteCache,而是生成一个新的对象,所以byte声明的时候,要采用直接装箱,或者除了new一个对象以外的方法。
  5)其他方法:

public static String toString(byte b) {  
    return Integer.toString((int)b, 10);  
}  
public String toString() {  
    return Integer.toString((int)value);  
}  
  
//decode 解码,将(2,8,16)进制转换为十进制  
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);  
}  
  
@Override  
public int hashCode() {  
    return Byte.hashCode(value);  
}  
public static int hashCode(byte value) {  
    return (int)value;  
}  
public boolean equals(Object obj) {  
    if (obj instanceof Byte) {  
        return value == ((Byte)obj).byteValue();  
    }  
    return false;  
}  
  
public static int toUnsignedInt(byte x) {  
    return ((int) x) & 0xff;  
}  
public static long toUnsignedLong(byte x) {  
    return ((long) x) & 0xffL;  
}  

  例子:

//其他方法  
Byte.toString(byte1);//1  
b8.toString();//1  
b8.hashCode();//1  
Byte.decode("0x8");//8  
b4.equals(b2);//true  
byte byte2 = -1;  
Byte.toUnsignedInt(byte1);//1  
Byte.toUnsignedLong(byte1);//1  
Byte.toUnsignedInt(byte2);//255 强转为int的-1,-1的二进制补码:1111,1111,1111,1111,1111,1111,1111,1111,进行& 0xff,截取byte表示8位以外的其他高位。  
Byte.toUnsignedLong(byte2);//255  

  

原文地址:https://www.cnblogs.com/knsbyoo/p/9032402.html