author | simonis |
Fri, 14 Aug 2015 10:35:45 +0200 | |
changeset 32209 | 24bb680a1609 |
parent 31671 | 362e0c0acece |
child 33519 | a33d1c19cbc8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
14507
diff
changeset
|
2 |
* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
28 |
import jdk.internal.HotSpotIntrinsicCandidate; |
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
29 |
|
2 | 30 |
/** |
31 |
* |
|
32 |
* The {@code Byte} class wraps a value of primitive type {@code byte} |
|
33 |
* in an object. An object of type {@code Byte} contains a single |
|
34 |
* field whose type is {@code byte}. |
|
35 |
* |
|
36 |
* <p>In addition, this class provides several methods for converting |
|
37 |
* a {@code byte} to a {@code String} and a {@code String} to a {@code |
|
38 |
* byte}, as well as other constants and methods useful when dealing |
|
39 |
* with a {@code byte}. |
|
40 |
* |
|
41 |
* @author Nakul Saraiya |
|
42 |
* @author Joseph D. Darcy |
|
43 |
* @see java.lang.Number |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
21334
diff
changeset
|
44 |
* @since 1.1 |
2 | 45 |
*/ |
46 |
public final class Byte extends Number implements Comparable<Byte> { |
|
47 |
||
48 |
/** |
|
49 |
* A constant holding the minimum value a {@code byte} can |
|
50 |
* have, -2<sup>7</sup>. |
|
51 |
*/ |
|
52 |
public static final byte MIN_VALUE = -128; |
|
53 |
||
54 |
/** |
|
55 |
* A constant holding the maximum value a {@code byte} can |
|
56 |
* have, 2<sup>7</sup>-1. |
|
57 |
*/ |
|
58 |
public static final byte MAX_VALUE = 127; |
|
59 |
||
60 |
/** |
|
61 |
* The {@code Class} instance representing the primitive type |
|
62 |
* {@code byte}. |
|
63 |
*/ |
|
11275 | 64 |
@SuppressWarnings("unchecked") |
2 | 65 |
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte"); |
66 |
||
67 |
/** |
|
68 |
* Returns a new {@code String} object representing the |
|
69 |
* specified {@code byte}. The radix is assumed to be 10. |
|
70 |
* |
|
71 |
* @param b the {@code byte} to be converted |
|
72 |
* @return the string representation of the specified {@code byte} |
|
73 |
* @see java.lang.Integer#toString(int) |
|
74 |
*/ |
|
75 |
public static String toString(byte b) { |
|
76 |
return Integer.toString((int)b, 10); |
|
77 |
} |
|
78 |
||
79 |
private static class ByteCache { |
|
80 |
private ByteCache(){} |
|
81 |
||
82 |
static final Byte cache[] = new Byte[-(-128) + 127 + 1]; |
|
83 |
||
84 |
static { |
|
85 |
for(int i = 0; i < cache.length; i++) |
|
86 |
cache[i] = new Byte((byte)(i - 128)); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Returns a {@code Byte} instance representing the specified |
|
92 |
* {@code byte} value. |
|
93 |
* If a new {@code Byte} instance is not required, this method |
|
94 |
* should generally be used in preference to the constructor |
|
95 |
* {@link #Byte(byte)}, as this method is likely to yield |
|
3224
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2
diff
changeset
|
96 |
* significantly better space and time performance since |
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2
diff
changeset
|
97 |
* all byte values are cached. |
2 | 98 |
* |
99 |
* @param b a byte value. |
|
100 |
* @return a {@code Byte} instance representing {@code b}. |
|
101 |
* @since 1.5 |
|
102 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
103 |
@HotSpotIntrinsicCandidate |
2 | 104 |
public static Byte valueOf(byte b) { |
105 |
final int offset = 128; |
|
106 |
return ByteCache.cache[(int)b + offset]; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Parses the string argument as a signed {@code byte} in the |
|
111 |
* radix specified by the second argument. The characters in the |
|
112 |
* string must all be digits, of the specified radix (as |
|
113 |
* determined by whether {@link java.lang.Character#digit(char, |
|
114 |
* int)} returns a nonnegative value) except that the first |
|
115 |
* character may be an ASCII minus sign {@code '-'} |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
116 |
* ({@code '\u005Cu002D'}) to indicate a negative value or an |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
117 |
* ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to |
2 | 118 |
* indicate a positive value. The resulting {@code byte} value is |
119 |
* returned. |
|
120 |
* |
|
121 |
* <p>An exception of type {@code NumberFormatException} is |
|
122 |
* thrown if any of the following situations occurs: |
|
123 |
* <ul> |
|
124 |
* <li> The first argument is {@code null} or is a string of |
|
125 |
* length zero. |
|
126 |
* |
|
127 |
* <li> The radix is either smaller than {@link |
|
128 |
* java.lang.Character#MIN_RADIX} or larger than {@link |
|
129 |
* java.lang.Character#MAX_RADIX}. |
|
130 |
* |
|
131 |
* <li> Any character of the string is not a digit of the |
|
132 |
* specified radix, except that the first character may be a minus |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
133 |
* sign {@code '-'} ({@code '\u005Cu002D'}) or plus sign |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
134 |
* {@code '+'} ({@code '\u005Cu002B'}) provided that the |
2 | 135 |
* string is longer than length 1. |
136 |
* |
|
137 |
* <li> The value represented by the string is not a value of type |
|
138 |
* {@code byte}. |
|
139 |
* </ul> |
|
140 |
* |
|
141 |
* @param s the {@code String} containing the |
|
142 |
* {@code byte} |
|
143 |
* representation to be parsed |
|
144 |
* @param radix the radix to be used while parsing {@code s} |
|
145 |
* @return the {@code byte} value represented by the string |
|
146 |
* argument in the specified radix |
|
147 |
* @throws NumberFormatException If the string does |
|
148 |
* not contain a parsable {@code byte}. |
|
149 |
*/ |
|
150 |
public static byte parseByte(String s, int radix) |
|
151 |
throws NumberFormatException { |
|
152 |
int i = Integer.parseInt(s, radix); |
|
153 |
if (i < MIN_VALUE || i > MAX_VALUE) |
|
154 |
throw new NumberFormatException( |
|
155 |
"Value out of range. Value:\"" + s + "\" Radix:" + radix); |
|
156 |
return (byte)i; |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Parses the string argument as a signed decimal {@code |
|
161 |
* byte}. The characters in the string must all be decimal digits, |
|
162 |
* except that the first character may be an ASCII minus sign |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
163 |
* {@code '-'} ({@code '\u005Cu002D'}) to indicate a negative |
2 | 164 |
* value or an ASCII plus sign {@code '+'} |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
165 |
* ({@code '\u005Cu002B'}) to indicate a positive value. The |
2 | 166 |
* resulting {@code byte} value is returned, exactly as if the |
167 |
* argument and the radix 10 were given as arguments to the {@link |
|
168 |
* #parseByte(java.lang.String, int)} method. |
|
169 |
* |
|
170 |
* @param s a {@code String} containing the |
|
171 |
* {@code byte} representation to be parsed |
|
172 |
* @return the {@code byte} value represented by the |
|
173 |
* argument in decimal |
|
174 |
* @throws NumberFormatException if the string does not |
|
175 |
* contain a parsable {@code byte}. |
|
176 |
*/ |
|
177 |
public static byte parseByte(String s) throws NumberFormatException { |
|
178 |
return parseByte(s, 10); |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Returns a {@code Byte} object holding the value |
|
183 |
* extracted from the specified {@code String} when parsed |
|
184 |
* with the radix given by the second argument. The first argument |
|
185 |
* is interpreted as representing a signed {@code byte} in |
|
186 |
* the radix specified by the second argument, exactly as if the |
|
187 |
* argument were given to the {@link #parseByte(java.lang.String, |
|
188 |
* int)} method. The result is a {@code Byte} object that |
|
189 |
* represents the {@code byte} value specified by the string. |
|
190 |
* |
|
191 |
* <p> In other words, this method returns a {@code Byte} object |
|
192 |
* equal to the value of: |
|
193 |
* |
|
194 |
* <blockquote> |
|
195 |
* {@code new Byte(Byte.parseByte(s, radix))} |
|
196 |
* </blockquote> |
|
197 |
* |
|
198 |
* @param s the string to be parsed |
|
199 |
* @param radix the radix to be used in interpreting {@code s} |
|
200 |
* @return a {@code Byte} object holding the value |
|
201 |
* represented by the string argument in the |
|
202 |
* specified radix. |
|
203 |
* @throws NumberFormatException If the {@code String} does |
|
204 |
* not contain a parsable {@code byte}. |
|
205 |
*/ |
|
206 |
public static Byte valueOf(String s, int radix) |
|
207 |
throws NumberFormatException { |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3943
diff
changeset
|
208 |
return valueOf(parseByte(s, radix)); |
2 | 209 |
} |
210 |
||
211 |
/** |
|
212 |
* Returns a {@code Byte} object holding the value |
|
213 |
* given by the specified {@code String}. The argument is |
|
214 |
* interpreted as representing a signed decimal {@code byte}, |
|
215 |
* exactly as if the argument were given to the {@link |
|
216 |
* #parseByte(java.lang.String)} method. The result is a |
|
217 |
* {@code Byte} object that represents the {@code byte} |
|
218 |
* value specified by the string. |
|
219 |
* |
|
220 |
* <p> In other words, this method returns a {@code Byte} object |
|
221 |
* equal to the value of: |
|
222 |
* |
|
223 |
* <blockquote> |
|
224 |
* {@code new Byte(Byte.parseByte(s))} |
|
225 |
* </blockquote> |
|
226 |
* |
|
227 |
* @param s the string to be parsed |
|
228 |
* @return a {@code Byte} object holding the value |
|
229 |
* represented by the string argument |
|
230 |
* @throws NumberFormatException If the {@code String} does |
|
231 |
* not contain a parsable {@code byte}. |
|
232 |
*/ |
|
233 |
public static Byte valueOf(String s) throws NumberFormatException { |
|
234 |
return valueOf(s, 10); |
|
235 |
} |
|
236 |
||
237 |
/** |
|
238 |
* Decodes a {@code String} into a {@code Byte}. |
|
239 |
* Accepts decimal, hexadecimal, and octal numbers given by |
|
240 |
* the following grammar: |
|
241 |
* |
|
242 |
* <blockquote> |
|
243 |
* <dl> |
|
244 |
* <dt><i>DecodableString:</i> |
|
245 |
* <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> |
|
246 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i> |
|
247 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i> |
|
248 |
* <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i> |
|
249 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i> |
|
21334 | 250 |
* |
2 | 251 |
* <dt><i>Sign:</i> |
252 |
* <dd>{@code -} |
|
253 |
* <dd>{@code +} |
|
254 |
* </dl> |
|
255 |
* </blockquote> |
|
256 |
* |
|
257 |
* <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
258 |
* are as defined in section 3.10.1 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
259 |
* <cite>The Java™ Language Specification</cite>, |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
260 |
* except that underscores are not accepted between digits. |
2 | 261 |
* |
262 |
* <p>The sequence of characters following an optional |
|
263 |
* sign and/or radix specifier ("{@code 0x}", "{@code 0X}", |
|
264 |
* "{@code #}", or leading zero) is parsed as by the {@code |
|
265 |
* Byte.parseByte} method with the indicated radix (10, 16, or 8). |
|
266 |
* This sequence of characters must represent a positive value or |
|
267 |
* a {@link NumberFormatException} will be thrown. The result is |
|
268 |
* negated if first character of the specified {@code String} is |
|
269 |
* the minus sign. No whitespace characters are permitted in the |
|
270 |
* {@code String}. |
|
271 |
* |
|
272 |
* @param nm the {@code String} to decode. |
|
273 |
* @return a {@code Byte} object holding the {@code byte} |
|
274 |
* value represented by {@code nm} |
|
275 |
* @throws NumberFormatException if the {@code String} does not |
|
276 |
* contain a parsable {@code byte}. |
|
277 |
* @see java.lang.Byte#parseByte(java.lang.String, int) |
|
278 |
*/ |
|
279 |
public static Byte decode(String nm) throws NumberFormatException { |
|
280 |
int i = Integer.decode(nm); |
|
281 |
if (i < MIN_VALUE || i > MAX_VALUE) |
|
282 |
throw new NumberFormatException( |
|
283 |
"Value " + i + " out of range from input " + nm); |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3943
diff
changeset
|
284 |
return valueOf((byte)i); |
2 | 285 |
} |
286 |
||
287 |
/** |
|
288 |
* The value of the {@code Byte}. |
|
289 |
* |
|
290 |
* @serial |
|
291 |
*/ |
|
292 |
private final byte value; |
|
293 |
||
294 |
/** |
|
295 |
* Constructs a newly allocated {@code Byte} object that |
|
296 |
* represents the specified {@code byte} value. |
|
297 |
* |
|
298 |
* @param value the value to be represented by the |
|
299 |
* {@code Byte}. |
|
300 |
*/ |
|
301 |
public Byte(byte value) { |
|
302 |
this.value = value; |
|
303 |
} |
|
304 |
||
305 |
/** |
|
306 |
* Constructs a newly allocated {@code Byte} object that |
|
307 |
* represents the {@code byte} value indicated by the |
|
308 |
* {@code String} parameter. The string is converted to a |
|
309 |
* {@code byte} value in exactly the manner used by the |
|
310 |
* {@code parseByte} method for radix 10. |
|
311 |
* |
|
312 |
* @param s the {@code String} to be converted to a |
|
313 |
* {@code Byte} |
|
314 |
* @throws NumberFormatException If the {@code String} |
|
315 |
* does not contain a parsable {@code byte}. |
|
316 |
* @see java.lang.Byte#parseByte(java.lang.String, int) |
|
317 |
*/ |
|
318 |
public Byte(String s) throws NumberFormatException { |
|
319 |
this.value = parseByte(s, 10); |
|
320 |
} |
|
321 |
||
322 |
/** |
|
323 |
* Returns the value of this {@code Byte} as a |
|
324 |
* {@code byte}. |
|
325 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
25859
diff
changeset
|
326 |
@HotSpotIntrinsicCandidate |
2 | 327 |
public byte byteValue() { |
328 |
return value; |
|
329 |
} |
|
330 |
||
331 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
332 |
* Returns the value of this {@code Byte} as a {@code short} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
333 |
* a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
334 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 335 |
*/ |
336 |
public short shortValue() { |
|
337 |
return (short)value; |
|
338 |
} |
|
339 |
||
340 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
341 |
* Returns the value of this {@code Byte} as an {@code int} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
342 |
* a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
343 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 344 |
*/ |
345 |
public int intValue() { |
|
346 |
return (int)value; |
|
347 |
} |
|
348 |
||
349 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
350 |
* Returns the value of this {@code Byte} as a {@code long} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
351 |
* a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
352 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 353 |
*/ |
354 |
public long longValue() { |
|
355 |
return (long)value; |
|
356 |
} |
|
357 |
||
358 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
359 |
* Returns the value of this {@code Byte} as a {@code float} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
360 |
* a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
361 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 362 |
*/ |
363 |
public float floatValue() { |
|
364 |
return (float)value; |
|
365 |
} |
|
366 |
||
367 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
368 |
* Returns the value of this {@code Byte} as a {@code double} |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
369 |
* after a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
370 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 371 |
*/ |
372 |
public double doubleValue() { |
|
373 |
return (double)value; |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Returns a {@code String} object representing this |
|
378 |
* {@code Byte}'s value. The value is converted to signed |
|
379 |
* decimal representation and returned as a string, exactly as if |
|
380 |
* the {@code byte} value were given as an argument to the |
|
381 |
* {@link java.lang.Byte#toString(byte)} method. |
|
382 |
* |
|
383 |
* @return a string representation of the value of this object in |
|
384 |
* base 10. |
|
385 |
*/ |
|
386 |
public String toString() { |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3943
diff
changeset
|
387 |
return Integer.toString((int)value); |
2 | 388 |
} |
389 |
||
390 |
/** |
|
3942
685e04a98396
4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
3288
diff
changeset
|
391 |
* Returns a hash code for this {@code Byte}; equal to the result |
685e04a98396
4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
3288
diff
changeset
|
392 |
* of invoking {@code intValue()}. |
685e04a98396
4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
3288
diff
changeset
|
393 |
* |
685e04a98396
4245470: algorithm of java.lang.Byte.hashCode() is not specified
martin
parents:
3288
diff
changeset
|
394 |
* @return a hash code value for this {@code Byte} |
2 | 395 |
*/ |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
396 |
@Override |
2 | 397 |
public int hashCode() { |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
398 |
return Byte.hashCode(value); |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
399 |
} |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
400 |
|
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
401 |
/** |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
402 |
* Returns a hash code for a {@code byte} value; compatible with |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
403 |
* {@code Byte.hashCode()}. |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
404 |
* |
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
14507
diff
changeset
|
405 |
* @param value the value to hash |
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
14507
diff
changeset
|
406 |
* @return a hash code value for a {@code byte} value. |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
407 |
* @since 1.8 |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
408 |
*/ |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
11676
diff
changeset
|
409 |
public static int hashCode(byte value) { |
2 | 410 |
return (int)value; |
411 |
} |
|
412 |
||
413 |
/** |
|
414 |
* Compares this object to the specified object. The result is |
|
415 |
* {@code true} if and only if the argument is not |
|
416 |
* {@code null} and is a {@code Byte} object that |
|
417 |
* contains the same {@code byte} value as this object. |
|
418 |
* |
|
419 |
* @param obj the object to compare with |
|
420 |
* @return {@code true} if the objects are the same; |
|
421 |
* {@code false} otherwise. |
|
422 |
*/ |
|
423 |
public boolean equals(Object obj) { |
|
424 |
if (obj instanceof Byte) { |
|
425 |
return value == ((Byte)obj).byteValue(); |
|
426 |
} |
|
427 |
return false; |
|
428 |
} |
|
429 |
||
430 |
/** |
|
431 |
* Compares two {@code Byte} objects numerically. |
|
432 |
* |
|
433 |
* @param anotherByte the {@code Byte} to be compared. |
|
434 |
* @return the value {@code 0} if this {@code Byte} is |
|
435 |
* equal to the argument {@code Byte}; a value less than |
|
436 |
* {@code 0} if this {@code Byte} is numerically less |
|
437 |
* than the argument {@code Byte}; and a value greater than |
|
438 |
* {@code 0} if this {@code Byte} is numerically |
|
439 |
* greater than the argument {@code Byte} (signed |
|
440 |
* comparison). |
|
441 |
* @since 1.2 |
|
442 |
*/ |
|
443 |
public int compareTo(Byte anotherByte) { |
|
3943
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
444 |
return compare(this.value, anotherByte.value); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
445 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
446 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
447 |
/** |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
448 |
* Compares two {@code byte} values numerically. |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
449 |
* The value returned is identical to what would be returned by: |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
450 |
* <pre> |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
451 |
* Byte.valueOf(x).compareTo(Byte.valueOf(y)) |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
452 |
* </pre> |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
453 |
* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
454 |
* @param x the first {@code byte} to compare |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
455 |
* @param y the second {@code byte} to compare |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
456 |
* @return the value {@code 0} if {@code x == y}; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
457 |
* a value less than {@code 0} if {@code x < y}; and |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
458 |
* a value greater than {@code 0} if {@code x > y} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
459 |
* @since 1.7 |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
460 |
*/ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
461 |
public static int compare(byte x, byte y) { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3942
diff
changeset
|
462 |
return x - y; |
2 | 463 |
} |
464 |
||
465 |
/** |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
466 |
* Converts the argument to an {@code int} by an unsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
467 |
* conversion. In an unsigned conversion to an {@code int}, the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
468 |
* high-order 24 bits of the {@code int} are zero and the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
469 |
* low-order 8 bits are equal to the bits of the {@code byte} argument. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
470 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
471 |
* Consequently, zero and positive {@code byte} values are mapped |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
472 |
* to a numerically equal {@code int} value and negative {@code |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
473 |
* byte} values are mapped to an {@code int} value equal to the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
474 |
* input plus 2<sup>8</sup>. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
475 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
476 |
* @param x the value to convert to an unsigned {@code int} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
477 |
* @return the argument converted to {@code int} by an unsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
478 |
* conversion |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
479 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
480 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
481 |
public static int toUnsignedInt(byte x) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
482 |
return ((int) x) & 0xff; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
483 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
484 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
485 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
486 |
* Converts the argument to a {@code long} by an unsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
487 |
* conversion. In an unsigned conversion to a {@code long}, the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
488 |
* high-order 56 bits of the {@code long} are zero and the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
489 |
* low-order 8 bits are equal to the bits of the {@code byte} argument. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
490 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
491 |
* Consequently, zero and positive {@code byte} values are mapped |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
492 |
* to a numerically equal {@code long} value and negative {@code |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
493 |
* byte} values are mapped to a {@code long} value equal to the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
494 |
* input plus 2<sup>8</sup>. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
495 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
496 |
* @param x the value to convert to an unsigned {@code long} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
497 |
* @return the argument converted to {@code long} by an unsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
498 |
* conversion |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
499 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
500 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
501 |
public static long toUnsignedLong(byte x) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
502 |
return ((long) x) & 0xffL; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
503 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
504 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
505 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
506 |
/** |
2 | 507 |
* The number of bits used to represent a {@code byte} value in two's |
508 |
* complement binary form. |
|
509 |
* |
|
510 |
* @since 1.5 |
|
511 |
*/ |
|
512 |
public static final int SIZE = 8; |
|
513 |
||
14507
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
514 |
/** |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
515 |
* The number of bytes used to represent a {@code byte} value in two's |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
516 |
* complement binary form. |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
517 |
* |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
518 |
* @since 1.8 |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
519 |
*/ |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
520 |
public static final int BYTES = SIZE / Byte.SIZE; |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
521 |
|
2 | 522 |
/** use serialVersionUID from JDK 1.1. for interoperability */ |
523 |
private static final long serialVersionUID = -7183698231559129828L; |
|
524 |
} |