<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### Byte源碼 ``` /** * Byte提供相應的轉換方法 * Byte繼承抽象類Number,實現了抽象類的方法,該抽象類多數方法都是用戶byte轉換為其他基礎類型的方法 * Byte實現Comparable接口,對接口里的比較方法進行了實現 */ public final class Byte extends Number implements Comparable<Byte> { /** * byte的最小值為-128,為-2的8次方,被final修飾說明不可變 */ public static final byte MIN_VALUE = -128; /** * byte最大值為127,表示int最大值為2的8次方減1 */ public static final byte MAX_VALUE = 127; /** * 獲取byte的字節碼 */ public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte"); /** * 調用Integer的toString方法將byte值轉換為String類型 */ public static String toString(byte b) { return Integer.toString((int)b, 10); } /** * 當加載Byte類時進行加載,將-128到127加載入內存中,使其唯一 */ private static class ByteCache { private ByteCache(){} //這是為了表名是從-128到127,后面的1位是數字0 static final Byte cache[] = new Byte[-(-128) + 127 + 1]; //靜態代碼塊,類加載時加載,初始化cache中元素 static { for(int i = 0; i < cache.length; i++) // 給這個數組賦值(-128到127) cache[i] = new Byte((byte)(i - 128)); } } /** * byte類型轉換為Byte類型,返回值直接從內存中讀取 */ public static Byte valueOf(byte b) { final int offset = 128; return ByteCache.cache[(int)b + offset]; } /** * 將字符串類型值轉換為byte類型,轉換失敗拋出轉換異常 * 調用Integer的轉換方法,將其轉換為int類型后判斷是否在byte區間內,最后返回byte值 */ 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; } /** * 調用上方parseByte方法,將String類型值轉換為byte類型 */ public static byte parseByte(String s) throws NumberFormatException { return parseByte(s, 10); } /** * 調用parseByte和valueOf方法,將String類型值轉換為Byte類型 */ public static Byte valueOf(String s, int radix) throws NumberFormatException { return valueOf(parseByte(s, radix)); } /** * 調用valueOf方法,將String類型值轉換為Byte */ public static Byte valueOf(String s) throws NumberFormatException { return valueOf(s, 10); } /** * 將字符串轉換為字節(它會根據實際情況進行解碼,默認會處理成十進制,0開頭的都會處理成8進制,0x和#開頭的都會處理成十六進制) */ public static Byte decode(String nm) throws NumberFormatException { int i = Integer.decode(nm); //轉換后不在區間內則拋出異常,否則返回Byte值 if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException( "Value " + i + " out of range from input " + nm); return valueOf((byte)i); } /** * Byte類型的值 */ private final byte value; /** * Byte無參構造,用于創建一個Byte對象 */ public Byte(byte value) { this.value = value; } /** * Byte有參構造,傳入String類型參數,用于創建Byte對象 */ public Byte(String s) throws NumberFormatException { this.value = parseByte(s, 10); } /** * 獲得當前Byte的值 */ public byte byteValue() { return value; } /** * 重寫父類方法,將byte值轉換為short類型 */ public short shortValue() { return (short)value; } /** * 重寫父類方法,將byte值轉換為int類型 */ public int intValue() { return (int)value; } /** * 重寫父類方法,將byte值轉換為int類型 */ public long longValue() { return (long)value; } /** * 重寫父類方法,將byte值轉換為float類型 */ public float floatValue() { return (float)value; } /** * 重寫父類方法,將byte值轉換為double類型 */ public double doubleValue() { return (double)value; } /** * 調用Integer的toString方法,將值轉換為String類型 */ public String toString() { return Integer.toString((int)value); } /** * 計算哈希值 */ public int hashCode() { return (int)value; } /** * 用于對象的比較 */ public boolean equals(Object obj) { //當obj為Byte類型時,比較value值是否相等 if (obj instanceof Byte) { return value == ((Byte)obj).byteValue(); } return false; } /** * 調用下方compare方法,用于比較值是否相等 */ public int compareTo(Byte anotherByte) { return compare(this.value, anotherByte.value); } /** * 當x<y時,返回值-1 * 當x=y時,返回值0 * 當x>y時,返回值1 */ public static int compare(byte x, byte y) { return x - y; } /** * 用于表示二進制byte值的大小,為8位 */ public static final int SIZE = 8; /** 版本號 */ private static final long serialVersionUID = -7183698231559129828L; } ``` ByteCache是Byte的一個內部類,里面穿件了一個數組,大小是-(-128) + 127 + 1 =256個,值是在-128到127之間,我們只要實例化了256個Byte對象就可以表示所有的byte值。因為這些都是靜態的,會避免回收和重復的實例化 測試: ``` Byte aByte1 = Byte.valueOf((byte) 11111); Byte aByte3 = Byte.valueOf((byte) 11111); //上面這個2個利用緩存機制,所以就是一個對象,而且地址和內容都一樣 Byte aByte2 = new Byte("103");//新創建一個對象,但是內容和aByte1和aByte3是一樣的 System.out.println(aByte1==aByte2); // false System.out.println(aByte1.equals(aByte2)); // true aByte2 = Byte.valueOf((byte) 11111); System.out.println(aByte1); // 103 System.out.println(aByte1==aByte2); // true System.out.println(aByte1==aByte3); // true System.out.println(aByte1.equals(aByte2)); // true ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看