等牛奶的咖啡

          經營屬于我們的咖啡屋

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            0 隨筆 :: 8 文章 :: 0 評論 :: 0 Trackbacks
            1 /**
            2  * 提供程序設計的基礎類
            3  */
            4 package java.lang;
            5 
            6 /**
            7  * 將一個基本類型 byte 的值包裝在一個 Byte 對象中
            8  */
            9 public final class Byte extends Number implements Comparable<Byte> {
           10 
           11     /** 常量,表示 byte 類型的最小值 */
           12     public static final byte MIN_VALUE = -128;
           13 
           14     /** 常量,表示 byte 類型的最大值 */
           15     public static final byte MAX_VALUE = 127;
           16 
           17     /** 表示基本類型 byte 的 Class 對象 */
           18     public static final Class<Byte> TYPE = (Class<Byte>) Class
           19             .getPrimitiveClass("byte");
           20 
           21     /** 用于以二進制補碼形式表示 byte 值的位數 */
           22     public static final int SIZE = 8;
           23 
           24     /** 定義一個私有變量,類型為 byte */
           25     private final byte value;
           26 
           27     /** 表明類的不同版本間的兼容性 */
           28     private static final long serialVersionUID = -7183698231559129828L;
           29 
           30     /**
           31      * 構造器,參數為基本類型 byte
           32      */
           33     public Byte(byte value) {
           34         this.value = value;
           35     }
           36 
           37     /**
           38      * 構造器,參數為 String 該字符串要存在 Byte 類型的范圍
           39      */
           40     public Byte(String s) throws NumberFormatException {
           41         this.value = parseByte(s, 10);
           42     }
           43 
           44     /**
           45      * 將 Byte 對象的值作為基本類型 byte 輸出
           46      * 覆蓋了父類 Number 中的 byteValue() 方法
           47      */
           48     public byte byteValue() {
           49         return value;
           50     }
           51 
           52     /**
           53      * 將 Byte 對象強制轉換為基本類型 short 輸出
           54      * 覆蓋了父類 Number 中的 shortValue() 方法
           55      */
           56     public short shortValue() {
           57         return (short) value;
           58     }
           59 
           60     /**
           61      * 將 Byte 對象強制轉換為基本類型 int 輸出
           62      * 定義了父類 Number 中的抽象方法 intValue()
           63      */
           64     public int intValue() {
           65         return (int) value;
           66     }
           67 
           68     /**
           69      * 將 Byte 對象強制轉換為基本類型 long 輸出
           70      * 定義了父類 Number 中的抽象方法 intValue()
           71      */
           72     public long longValue() {
           73         return (long) value;
           74     }
           75 
           76     /**
           77      * 將 Byte 對象強制轉換為基本類型 float 輸出
           78      * 定義了父類 Number 中的抽象方法 floatValue()
           79      */
           80     public float floatValue() {
           81         return (float) value;
           82     }
           83 
           84     /**
           85      * 將 Byte 對象強制轉換為基本類型 double 輸出
           86      * 定義了父類 Number 中的抽象方法 doubleValue()
           87      */
           88     public double doubleValue() {
           89         return (double) value;
           90     }
           91 
           92     /**
           93      * 返回表示指定 byte 的 String 對象,以基數為 10 計算
           94      */
           95     public static String toString(byte b) {
           96         return Integer.toString((int) b, 10);
           97     }
           98 
           99     /**
          100      * 返回表示此 Byte 對象值的 String 對象
          101      */
          102     public String toString() {
          103         return String.valueOf((int) value);
          104     }
          105 
          106     /**
          107      * 內部類 ByteCache 準備把所有256個 byte 存在緩存里
          108      */
          109     private static class ByteCache {
          110         private ByteCache() {
          111         }
          112 
          113         static final Byte cache[] = new Byte[-(-128+ 127 + 1];
          114 
          115         static {
          116             for (int i = 0; i < cache.length; i++)
          117                 cache[i] = new Byte((byte) (i - 128));
          118         }
          119     }
          120 
          121     /**
          122      * 返回一個表示基本類型 byte 值的 Byte 對象
          123      * 直接從內部類中取,比構造器效率高
          124      */
          125     public static Byte valueOf(byte b) {
          126         final int offset = 128;
          127         return ByteCache.cache[(int) b + offset];
          128     }
          129 
          130     /**
          131      * 將 String 對象解析為有符號的 10 進制的 Byte 對象,第一個參數為 String ,第二個參數為基數,范圍[2,36]
          132      */
          133     public static Byte valueOf(String s, int radix)
          134             throws NumberFormatException {
          135         return new Byte(parseByte(s, radix));
          136     }
          137 
          138     /**
          139      * 將 String 對象解析為有符號的 10 進制基本類型 byte
          140      */
          141     public static Byte valueOf(String s) throws NumberFormatException {
          142         return valueOf(s, 10);
          143     }
          144 
          145     /**
          146      * 將 String 對象解析為有符號的 10 進制基本類型 byte
          147      */
          148     public static byte parseByte(String s) throws NumberFormatException {
          149         return parseByte(s, 10);
          150     }
          151 
          152     /**
          153      * 將 String 對象解析為有符號的 10 進制基本類型 byte 第一個參數為 String ,第二個參數為基數,范圍[2,36]
          154      * 調用的主要方法是 Integer.parseInt()
          155      * 由第二個參數指定基數
          156      */
          157     public static byte parseByte(String s, int radix)
          158             throws NumberFormatException {
          159         int i = Integer.parseInt(s, radix);
          160         if (i < MIN_VALUE || i > MAX_VALUE)
          161             throw new NumberFormatException("Value out of range. Value:\"" + s
          162                     + "\" Radix:" + radix);
          163         return (byte) i;
          164     }
          165 
          166     /**
          167      * 將 String 對象解析為有符號的 10 進制基本類型 byte ,String 對象前的 - 對應負數 0x 0X # 對應 16 進制 0
          168      * 對應 8 進制
          169      * 直接由字符串的前綴來判斷該字符串的類型
          170      * 最終還是調用 parseByte() 轉到調用 Integer.parseInt()
          171      */
          172     public static Byte decode(String nm) throws NumberFormatException {
          173         /** 用于確定基數 **/
          174         int radix = 10;
          175         /** 用于定位數值部分開始的位置 **/
          176         int index = 0;
          177         /** 用于確定 正負 **/
          178         boolean negative = false;
          179         Byte result;
          180 
          181         /** 定位數值部分開始的位置 **/
          182         if (nm.startsWith("-")) {
          183             negative = true;
          184             index++;
          185         }
          186         if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
          187             index += 2;
          188             radix = 16;
          189         } else if (nm.startsWith("#", index)) {
          190             index++;
          191             radix = 16;
          192         } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
          193             index++;
          194             radix = 8;
          195         }
          196         if (nm.startsWith("-", index))
          197             throw new NumberFormatException("Negative sign in wrong position");
          198 
          199         /** 調用 valueOf()方法進行解析 **/
          200         try {
          201             result = Byte.valueOf(nm.substring(index), radix);
          202             result = negative ? new Byte((byte-result.byteValue()) : result;
          203         } catch (NumberFormatException e) {
          204             String constant = negative ? new String("-" + nm.substring(index))
          205                     : nm.substring(index);
          206             result = Byte.valueOf(constant, radix);
          207         }
          208         return result;
          209     }
          210 
          211     /**
          212      * 返回此 Byte 的哈希碼 即將 Byte 對象的值強制轉換成基本類型 int
          213      */
          214     public int hashCode() {
          215         return (int) value;
          216     }
          217 
          218     /**
          219      * 比較兩個 Byte 對象是否相同 當且僅當參數是一個與此對象一樣,都表示同一個 byte 值的 Byte 對象時,才返回 true
          220      */
          221     public boolean equals(Object obj) {
          222         if (obj instanceof Byte) {
          223             return value == ((Byte) obj).byteValue();
          224         }
          225         return false;
          226     }
          227 
          228     /**
          229      * 將此 Byte 實例與其他 Byte 實例進行比較,true 為 0 false 為 非0
          230      */
          231     public int compareTo(Byte anotherByte) {
          232         return this.value - anotherByte.value;
          233     }
          234 }
          235 
          posted on 2009-10-30 09:16 等牛奶的咖啡 閱讀(732) 評論(0)  編輯  收藏 所屬分類: JDK源代碼
          主站蜘蛛池模板: 冷水江市| 磴口县| 益阳市| 井研县| 龙川县| 左云县| 镇坪县| 喀喇| 布尔津县| 资阳市| 庆城县| 军事| 玉树县| 大田县| 仪征市| 浦东新区| 衡南县| 嘉兴市| 敖汉旗| 旬阳县| 南丹县| 哈尔滨市| 金秀| 新郑市| 南和县| 常德市| 台北县| 遂溪县| 临城县| 崇左市| 景谷| 建宁县| 大理市| 通道| 珲春市| 日照市| 荔浦县| 兴安县| 饶平县| 大新县| 武冈市|