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 valuepublic 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)其他方法:
例子:
//其他方法 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
總結
以上是生活随笔為你收集整理的JDK源码分析:Byte.java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux基础命令篇一
- 下一篇: NVIDIA | 一种重建照片的 AI