1 /**
2 * 提供程序設計的基礎類
3 */
4 package java.lang;
5
6 /**
7 * 將一個基本類型 short 的值包裝在一個 Short 對象中
8 */
9 public final class Short extends Number implements Comparable<Short> {
10
11 /** 常量,表示 short 類型的最小值 * */
12 public static final short MIN_VALUE = -32768;
13
14 /** 常量,表示 short 類型的最大值 * */
15 public static final short MAX_VALUE = 32767;
16
17 /** 用于以二進制補碼形式表示 short 值的位數 */
18 public static final int SIZE = 16;
19
20 /** 表示基本類型 short 的 Class 對象 */
21 public static final Class<Short> TYPE = (Class<Short>) Class
22 .getPrimitiveClass("short");
23
24 /** 定義一個私有變量,類型為 short */
25 private final short value;
26
27 /** 表明類的不同版本間的兼容性 */
28 private static final long serialVersionUID = 7515723908773894738L;
29
30 /**
31 * 構造器,參數為基本類型 short
32 */
33 public Short(short value) {
34 this.value = value;
35 }
36
37 /**
38 * 構造器,參數為 String 該字符串要存在 Short 類型的范圍
39 */
40 public Short(String s) throws NumberFormatException {
41 this.value = parseShort(s, 10);
42 }
43
44 /**
45 * 將 Short 對象強制轉換為基本類型 byte 輸出
46 * 覆蓋了父類 Number 中的 byteValue() 方法
47 */
48 public byte byteValue() {
49 return (byte) value;
50 }
51
52 /**
53 * 將 Short 對象的值作為基本類型 short 輸出
54 * 覆蓋了父類 Number 中的 shortValue() 方法
55 */
56 public short shortValue() {
57 return value;
58 }
59
60 /**
61 * 將 Short 對象強制轉換為基本類型 int 輸出
62 * 定義了父類 Number 中的抽象方法 intValue()
63 */
64 public int intValue() {
65 return (int) value;
66 }
67
68 /**
69 * 將 Short 對象強制轉換為基本類型 long 輸出
70 * 定義了父類 Number 中的抽象方法 longValue()
71 */
72 public long longValue() {
73 return (long) value;
74 }
75
76 /**
77 * 將 Short 對象強制轉換為基本類型 float 輸出
78 * 定義了父類 Number 中的抽象方法 floatValue()
79 */
80 public float floatValue() {
81 return (float) value;
82 }
83
84 /**
85 * 將 Short 對象強制轉換為基本類型 double 輸出
86 * 定義了父類 Number 中的抽象方法 doubleValue()
87 */
88 public double doubleValue() {
89 return (double) value;
90 }
91
92 /**
93 * 返回表示指定 short 的 String 對象,以基數為 10 計算
94 */
95 public static String toString(short s) {
96 return Integer.toString((int) s, 10);
97 }
98
99 /**
100 * 返回表示此 Short 對象值的 String 對象
101 */
102 public String toString() {
103 return String.valueOf((int) value);
104 }
105
106 /**
107 * 內部類 ShortCache 準備把256個 short 存在緩存里
108 */
109 private static class ShortCache {
110 private ShortCache() {
111 }
112
113 static final Short cache[] = new Short[-(-128) + 127 + 1];
114
115 static {
116 for (int i = 0; i < cache.length; i++)
117 cache[i] = new Short((short) (i - 128));
118 }
119 }
120
121
122 /**
123 * 返回一個表示基本類型 short 值的 Short 對象
124 * 直接從內部類中取,比構造器效率高
125 */
126 public static Short valueOf(short s) {
127 final int offset = 128;
128 int sAsInt = s;
129 if (sAsInt >= -128 && sAsInt <= 127) {
130 return ShortCache.cache[sAsInt + offset];
131 }
132 return new Short(s);
133 }
134
135 /**
136 * 將 String 對象解析為有符號的 10 進制的 Short 對象,第一個參數為 String ,第二個參數為基數,范圍[2,36]
137 */
138 public static Short valueOf(String s, int radix)
139 throws NumberFormatException {
140 return new Short(parseShort(s, radix));
141 }
142
143 /**
144 * 將 String 對象解析為有符號的 10 進制基本類型 short
145 */
146 public static Short valueOf(String s) throws NumberFormatException {
147 return valueOf(s, 10);
148 }
149
150 /**
151 * 將 String 對象解析為有符號的 10 進制基本類型 short
152 */
153 public static short parseShort(String s) throws NumberFormatException {
154 return parseShort(s, 10);
155 }
156
157 /**
158 * 將 String 對象解析為有符號的 10 進制基本類型 short 第一個參數為 String ,第二個參數為基數,范圍[2,36]
159 * 調用的主要方法是 Integer.parseInt()
160 * 由第二個參數指定基數
161 */
162 public static short parseShort(String s, int radix)
163 throws NumberFormatException {
164 int i = Integer.parseInt(s, radix);
165 if (i < MIN_VALUE || i > MAX_VALUE)
166 throw new NumberFormatException("Value out of range. Value:\"" + s
167 + "\" Radix:" + radix);
168 return (short) i;
169 }
170
171 /**
172 * 將 String 對象解析為有符號的 10 進制基本類型 short ,String 對象前的 - 對應負數 0x 0X # 對應 16 進制 0
173 * 對應 8 進制
174 * 直接由字符串的前綴來判斷該字符串的類型
175 * 最終還是調用 parseShort() 轉到調用 Integer.parseInt()
176 */
177 public static Short decode(String nm) throws NumberFormatException {
178 /** 用于確定基數 **/
179 int radix = 10;
180 /** 用于定位數值部分開始的位置 **/
181 int index = 0;
182 /** 用于確定 正負 **/
183 boolean negative = false;
184 Short result;
185
186 /** 定位數值部分開始的位置 **/
187 if (nm.startsWith("-")) {
188 negative = true;
189 index++;
190 }
191 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
192 index += 2;
193 radix = 16;
194 } else if (nm.startsWith("#", index)) {
195 index++;
196 radix = 16;
197 } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
198 index++;
199 radix = 8;
200 }
201
202 if (nm.startsWith("-", index))
203 throw new NumberFormatException("Negative sign in wrong position");
204
205 /** 調用 valueOf()方法進行解析 **/
206 try {
207 result = Short.valueOf(nm.substring(index), radix);
208 result = negative ? new Short((short) -result.shortValue())
209 : result;
210 } catch (NumberFormatException e) {
211 String constant = negative ? new String("-" + nm.substring(index))
212 : nm.substring(index);
213 result = Short.valueOf(constant, radix);
214 }
215 return result;
216 }
217
218 /**
219 * 返回此 Short 的哈希碼 即將 Short 對象的值強制轉換成基本類型 int
220 */
221 public int hashCode() {
222 return (int) value;
223 }
224
225 /**
226 * 比較兩個 Short 對象是否相同 當且僅當參數是一個與此對象一樣,都表示同一個 short 值的 Short 對象時,才返回 true
227 */
228 public boolean equals(Object obj) {
229 if (obj instanceof Short) {
230 return value == ((Short) obj).shortValue();
231 }
232 return false;
233 }
234
235 /**
236 * 將此 Short 實例與其他 Short 實例進行比較,true 為 0 false 為 非0
237 */
238 public int compareTo(Short anotherShort) {
239 return this.value - anotherShort.value;
240 }
241
242 /**
243 * 返回通過反轉(或者交換,效果相同)指定 short 值中的字節而獲得的值
244 * 按字節倒置 在網絡傳輸處理時特有用
245 */
246 public static short reverseBytes(short i) {
247 return (short) (((i & 0xFF00) >> 8) | (i << 8));
248 }
249 }
250
2 * 提供程序設計的基礎類
3 */
4 package java.lang;
5
6 /**
7 * 將一個基本類型 short 的值包裝在一個 Short 對象中
8 */
9 public final class Short extends Number implements Comparable<Short> {
10
11 /** 常量,表示 short 類型的最小值 * */
12 public static final short MIN_VALUE = -32768;
13
14 /** 常量,表示 short 類型的最大值 * */
15 public static final short MAX_VALUE = 32767;
16
17 /** 用于以二進制補碼形式表示 short 值的位數 */
18 public static final int SIZE = 16;
19
20 /** 表示基本類型 short 的 Class 對象 */
21 public static final Class<Short> TYPE = (Class<Short>) Class
22 .getPrimitiveClass("short");
23
24 /** 定義一個私有變量,類型為 short */
25 private final short value;
26
27 /** 表明類的不同版本間的兼容性 */
28 private static final long serialVersionUID = 7515723908773894738L;
29
30 /**
31 * 構造器,參數為基本類型 short
32 */
33 public Short(short value) {
34 this.value = value;
35 }
36
37 /**
38 * 構造器,參數為 String 該字符串要存在 Short 類型的范圍
39 */
40 public Short(String s) throws NumberFormatException {
41 this.value = parseShort(s, 10);
42 }
43
44 /**
45 * 將 Short 對象強制轉換為基本類型 byte 輸出
46 * 覆蓋了父類 Number 中的 byteValue() 方法
47 */
48 public byte byteValue() {
49 return (byte) value;
50 }
51
52 /**
53 * 將 Short 對象的值作為基本類型 short 輸出
54 * 覆蓋了父類 Number 中的 shortValue() 方法
55 */
56 public short shortValue() {
57 return value;
58 }
59
60 /**
61 * 將 Short 對象強制轉換為基本類型 int 輸出
62 * 定義了父類 Number 中的抽象方法 intValue()
63 */
64 public int intValue() {
65 return (int) value;
66 }
67
68 /**
69 * 將 Short 對象強制轉換為基本類型 long 輸出
70 * 定義了父類 Number 中的抽象方法 longValue()
71 */
72 public long longValue() {
73 return (long) value;
74 }
75
76 /**
77 * 將 Short 對象強制轉換為基本類型 float 輸出
78 * 定義了父類 Number 中的抽象方法 floatValue()
79 */
80 public float floatValue() {
81 return (float) value;
82 }
83
84 /**
85 * 將 Short 對象強制轉換為基本類型 double 輸出
86 * 定義了父類 Number 中的抽象方法 doubleValue()
87 */
88 public double doubleValue() {
89 return (double) value;
90 }
91
92 /**
93 * 返回表示指定 short 的 String 對象,以基數為 10 計算
94 */
95 public static String toString(short s) {
96 return Integer.toString((int) s, 10);
97 }
98
99 /**
100 * 返回表示此 Short 對象值的 String 對象
101 */
102 public String toString() {
103 return String.valueOf((int) value);
104 }
105
106 /**
107 * 內部類 ShortCache 準備把256個 short 存在緩存里
108 */
109 private static class ShortCache {
110 private ShortCache() {
111 }
112
113 static final Short cache[] = new Short[-(-128) + 127 + 1];
114
115 static {
116 for (int i = 0; i < cache.length; i++)
117 cache[i] = new Short((short) (i - 128));
118 }
119 }
120
121
122 /**
123 * 返回一個表示基本類型 short 值的 Short 對象
124 * 直接從內部類中取,比構造器效率高
125 */
126 public static Short valueOf(short s) {
127 final int offset = 128;
128 int sAsInt = s;
129 if (sAsInt >= -128 && sAsInt <= 127) {
130 return ShortCache.cache[sAsInt + offset];
131 }
132 return new Short(s);
133 }
134
135 /**
136 * 將 String 對象解析為有符號的 10 進制的 Short 對象,第一個參數為 String ,第二個參數為基數,范圍[2,36]
137 */
138 public static Short valueOf(String s, int radix)
139 throws NumberFormatException {
140 return new Short(parseShort(s, radix));
141 }
142
143 /**
144 * 將 String 對象解析為有符號的 10 進制基本類型 short
145 */
146 public static Short valueOf(String s) throws NumberFormatException {
147 return valueOf(s, 10);
148 }
149
150 /**
151 * 將 String 對象解析為有符號的 10 進制基本類型 short
152 */
153 public static short parseShort(String s) throws NumberFormatException {
154 return parseShort(s, 10);
155 }
156
157 /**
158 * 將 String 對象解析為有符號的 10 進制基本類型 short 第一個參數為 String ,第二個參數為基數,范圍[2,36]
159 * 調用的主要方法是 Integer.parseInt()
160 * 由第二個參數指定基數
161 */
162 public static short parseShort(String s, int radix)
163 throws NumberFormatException {
164 int i = Integer.parseInt(s, radix);
165 if (i < MIN_VALUE || i > MAX_VALUE)
166 throw new NumberFormatException("Value out of range. Value:\"" + s
167 + "\" Radix:" + radix);
168 return (short) i;
169 }
170
171 /**
172 * 將 String 對象解析為有符號的 10 進制基本類型 short ,String 對象前的 - 對應負數 0x 0X # 對應 16 進制 0
173 * 對應 8 進制
174 * 直接由字符串的前綴來判斷該字符串的類型
175 * 最終還是調用 parseShort() 轉到調用 Integer.parseInt()
176 */
177 public static Short decode(String nm) throws NumberFormatException {
178 /** 用于確定基數 **/
179 int radix = 10;
180 /** 用于定位數值部分開始的位置 **/
181 int index = 0;
182 /** 用于確定 正負 **/
183 boolean negative = false;
184 Short result;
185
186 /** 定位數值部分開始的位置 **/
187 if (nm.startsWith("-")) {
188 negative = true;
189 index++;
190 }
191 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
192 index += 2;
193 radix = 16;
194 } else if (nm.startsWith("#", index)) {
195 index++;
196 radix = 16;
197 } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
198 index++;
199 radix = 8;
200 }
201
202 if (nm.startsWith("-", index))
203 throw new NumberFormatException("Negative sign in wrong position");
204
205 /** 調用 valueOf()方法進行解析 **/
206 try {
207 result = Short.valueOf(nm.substring(index), radix);
208 result = negative ? new Short((short) -result.shortValue())
209 : result;
210 } catch (NumberFormatException e) {
211 String constant = negative ? new String("-" + nm.substring(index))
212 : nm.substring(index);
213 result = Short.valueOf(constant, radix);
214 }
215 return result;
216 }
217
218 /**
219 * 返回此 Short 的哈希碼 即將 Short 對象的值強制轉換成基本類型 int
220 */
221 public int hashCode() {
222 return (int) value;
223 }
224
225 /**
226 * 比較兩個 Short 對象是否相同 當且僅當參數是一個與此對象一樣,都表示同一個 short 值的 Short 對象時,才返回 true
227 */
228 public boolean equals(Object obj) {
229 if (obj instanceof Short) {
230 return value == ((Short) obj).shortValue();
231 }
232 return false;
233 }
234
235 /**
236 * 將此 Short 實例與其他 Short 實例進行比較,true 為 0 false 為 非0
237 */
238 public int compareTo(Short anotherShort) {
239 return this.value - anotherShort.value;
240 }
241
242 /**
243 * 返回通過反轉(或者交換,效果相同)指定 short 值中的字節而獲得的值
244 * 按字節倒置 在網絡傳輸處理時特有用
245 */
246 public static short reverseBytes(short i) {
247 return (short) (((i & 0xFF00) >> 8) | (i << 8));
248 }
249 }
250