等牛奶的咖啡

          經(jīng)營屬于我們的咖啡屋

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            0 隨筆 :: 8 文章 :: 0 評論 :: 0 Trackbacks
            1 /**
            2  * 提供程序設(shè)計的基礎(chǔ)類
            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 值的位數(shù) */
           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      * 構(gòu)造器,參數(shù)為基本類型 byte
           32      */
           33     public Byte(byte value) {
           34         this.value = value;
           35     }
           36 
           37     /**
           38      * 構(gòu)造器,參數(shù)為 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 對象強制轉(zhuǎn)換為基本類型 short 輸出
           54      * 覆蓋了父類 Number 中的 shortValue() 方法
           55      */
           56     public short shortValue() {
           57         return (short) value;
           58     }
           59 
           60     /**
           61      * 將 Byte 對象強制轉(zhuǎn)換為基本類型 int 輸出
           62      * 定義了父類 Number 中的抽象方法 intValue()
           63      */
           64     public int intValue() {
           65         return (int) value;
           66     }
           67 
           68     /**
           69      * 將 Byte 對象強制轉(zhuǎn)換為基本類型 long 輸出
           70      * 定義了父類 Number 中的抽象方法 intValue()
           71      */
           72     public long longValue() {
           73         return (long) value;
           74     }
           75 
           76     /**
           77      * 將 Byte 對象強制轉(zhuǎn)換為基本類型 float 輸出
           78      * 定義了父類 Number 中的抽象方法 floatValue()
           79      */
           80     public float floatValue() {
           81         return (float) value;
           82     }
           83 
           84     /**
           85      * 將 Byte 對象強制轉(zhuǎn)換為基本類型 double 輸出
           86      * 定義了父類 Number 中的抽象方法 doubleValue()
           87      */
           88     public double doubleValue() {
           89         return (double) value;
           90     }
           91 
           92     /**
           93      * 返回表示指定 byte 的 String 對象,以基數(shù)為 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      * 內(nèi)部類 ByteCache 準(zhǔn)備把所有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      * 直接從內(nèi)部類中取,比構(gòu)造器效率高
          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 對象,第一個參數(shù)為 String ,第二個參數(shù)為基數(shù),范圍[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 第一個參數(shù)為 String ,第二個參數(shù)為基數(shù),范圍[2,36]
          154      * 調(diào)用的主要方法是 Integer.parseInt()
          155      * 由第二個參數(shù)指定基數(shù)
          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 對象前的 - 對應(yīng)負數(shù) 0x 0X # 對應(yīng) 16 進制 0
          168      * 對應(yīng) 8 進制
          169      * 直接由字符串的前綴來判斷該字符串的類型
          170      * 最終還是調(diào)用 parseByte() 轉(zhuǎn)到調(diào)用 Integer.parseInt()
          171      */
          172     public static Byte decode(String nm) throws NumberFormatException {
          173         /** 用于確定基數(shù) **/
          174         int radix = 10;
          175         /** 用于定位數(shù)值部分開始的位置 **/
          176         int index = 0;
          177         /** 用于確定 正負 **/
          178         boolean negative = false;
          179         Byte result;
          180 
          181         /** 定位數(shù)值部分開始的位置 **/
          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         /** 調(diào)用 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 對象的值強制轉(zhuǎn)換成基本類型 int
          213      */
          214     public int hashCode() {
          215         return (int) value;
          216     }
          217 
          218     /**
          219      * 比較兩個 Byte 對象是否相同 當(dāng)且僅當(dāng)參數(shù)是一個與此對象一樣,都表示同一個 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 等牛奶的咖啡 閱讀(731) 評論(0)  編輯  收藏 所屬分類: JDK源代碼

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 察哈| 色达县| 双牌县| 凉城县| 台北市| 广水市| 南宫市| 郸城县| 马公市| 长丰县| 公安县| 东乌| 阿合奇县| 元氏县| 大港区| 城步| 洛扎县| 星座| 绥德县| 邵东县| 方正县| 镇原县| 牟定县| 兴仁县| 苏州市| 墨竹工卡县| 肥乡县| 砚山县| 红桥区| 安陆市| 会宁县| 红安县| 岐山县| 曲阳县| 义马市| 桃源县| 曲水县| 喀喇| 正宁县| 麦盖提县| 隆尧县|