等牛奶的咖啡

经营属于我们的咖啡屋

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  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 等牛奶的咖啡 阅读(713) 评论(0)  编辑  收藏 所属分类: JDK源代码

只有注册用户登录后才能发表评论。


网站导航: