jaroslav@67
|
1 |
/*
|
jaroslav@67
|
2 |
* Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@67
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@67
|
4 |
*
|
jaroslav@67
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@67
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@67
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@67
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@67
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@67
|
10 |
*
|
jaroslav@67
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@67
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@67
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@67
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@67
|
15 |
* accompanied this code).
|
jaroslav@67
|
16 |
*
|
jaroslav@67
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@67
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@67
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@67
|
20 |
*
|
jaroslav@67
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@67
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@67
|
23 |
* questions.
|
jaroslav@67
|
24 |
*/
|
jaroslav@67
|
25 |
|
jaroslav@67
|
26 |
package java.lang;
|
jaroslav@67
|
27 |
|
jaroslav@67
|
28 |
/**
|
jaroslav@67
|
29 |
*
|
jaroslav@67
|
30 |
* The {@code Byte} class wraps a value of primitive type {@code byte}
|
jaroslav@67
|
31 |
* in an object. An object of type {@code Byte} contains a single
|
jaroslav@67
|
32 |
* field whose type is {@code byte}.
|
jaroslav@67
|
33 |
*
|
jaroslav@67
|
34 |
* <p>In addition, this class provides several methods for converting
|
jaroslav@67
|
35 |
* a {@code byte} to a {@code String} and a {@code String} to a {@code
|
jaroslav@67
|
36 |
* byte}, as well as other constants and methods useful when dealing
|
jaroslav@67
|
37 |
* with a {@code byte}.
|
jaroslav@67
|
38 |
*
|
jaroslav@67
|
39 |
* @author Nakul Saraiya
|
jaroslav@67
|
40 |
* @author Joseph D. Darcy
|
jaroslav@67
|
41 |
* @see java.lang.Number
|
jaroslav@67
|
42 |
* @since JDK1.1
|
jaroslav@67
|
43 |
*/
|
jaroslav@67
|
44 |
public final class Byte extends Number implements Comparable<Byte> {
|
jaroslav@67
|
45 |
|
jaroslav@67
|
46 |
/**
|
jaroslav@67
|
47 |
* A constant holding the minimum value a {@code byte} can
|
jaroslav@67
|
48 |
* have, -2<sup>7</sup>.
|
jaroslav@67
|
49 |
*/
|
jaroslav@67
|
50 |
public static final byte MIN_VALUE = -128;
|
jaroslav@67
|
51 |
|
jaroslav@67
|
52 |
/**
|
jaroslav@67
|
53 |
* A constant holding the maximum value a {@code byte} can
|
jaroslav@67
|
54 |
* have, 2<sup>7</sup>-1.
|
jaroslav@67
|
55 |
*/
|
jaroslav@67
|
56 |
public static final byte MAX_VALUE = 127;
|
jaroslav@67
|
57 |
|
jaroslav@67
|
58 |
/**
|
jaroslav@67
|
59 |
* The {@code Class} instance representing the primitive type
|
jaroslav@67
|
60 |
* {@code byte}.
|
jaroslav@67
|
61 |
*/
|
jaroslav@67
|
62 |
public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
|
jaroslav@67
|
63 |
|
jaroslav@67
|
64 |
/**
|
jaroslav@67
|
65 |
* Returns a new {@code String} object representing the
|
jaroslav@67
|
66 |
* specified {@code byte}. The radix is assumed to be 10.
|
jaroslav@67
|
67 |
*
|
jaroslav@67
|
68 |
* @param b the {@code byte} to be converted
|
jaroslav@67
|
69 |
* @return the string representation of the specified {@code byte}
|
jaroslav@67
|
70 |
* @see java.lang.Integer#toString(int)
|
jaroslav@67
|
71 |
*/
|
jaroslav@67
|
72 |
public static String toString(byte b) {
|
jaroslav@67
|
73 |
return Integer.toString((int)b, 10);
|
jaroslav@67
|
74 |
}
|
jaroslav@67
|
75 |
|
jaroslav@67
|
76 |
private static class ByteCache {
|
jaroslav@67
|
77 |
private ByteCache(){}
|
jaroslav@67
|
78 |
|
jaroslav@67
|
79 |
static final Byte cache[] = new Byte[-(-128) + 127 + 1];
|
jaroslav@67
|
80 |
|
jaroslav@67
|
81 |
static {
|
jaroslav@67
|
82 |
for(int i = 0; i < cache.length; i++)
|
jaroslav@67
|
83 |
cache[i] = new Byte((byte)(i - 128));
|
jaroslav@67
|
84 |
}
|
jaroslav@67
|
85 |
}
|
jaroslav@67
|
86 |
|
jaroslav@67
|
87 |
/**
|
jaroslav@67
|
88 |
* Returns a {@code Byte} instance representing the specified
|
jaroslav@67
|
89 |
* {@code byte} value.
|
jaroslav@67
|
90 |
* If a new {@code Byte} instance is not required, this method
|
jaroslav@67
|
91 |
* should generally be used in preference to the constructor
|
jaroslav@67
|
92 |
* {@link #Byte(byte)}, as this method is likely to yield
|
jaroslav@67
|
93 |
* significantly better space and time performance since
|
jaroslav@67
|
94 |
* all byte values are cached.
|
jaroslav@67
|
95 |
*
|
jaroslav@67
|
96 |
* @param b a byte value.
|
jaroslav@67
|
97 |
* @return a {@code Byte} instance representing {@code b}.
|
jaroslav@67
|
98 |
* @since 1.5
|
jaroslav@67
|
99 |
*/
|
jaroslav@67
|
100 |
public static Byte valueOf(byte b) {
|
jaroslav@67
|
101 |
final int offset = 128;
|
jaroslav@67
|
102 |
return ByteCache.cache[(int)b + offset];
|
jaroslav@67
|
103 |
}
|
jaroslav@67
|
104 |
|
jaroslav@67
|
105 |
/**
|
jaroslav@67
|
106 |
* Parses the string argument as a signed {@code byte} in the
|
jaroslav@67
|
107 |
* radix specified by the second argument. The characters in the
|
jaroslav@67
|
108 |
* string must all be digits, of the specified radix (as
|
jaroslav@67
|
109 |
* determined by whether {@link java.lang.Character#digit(char,
|
jaroslav@67
|
110 |
* int)} returns a nonnegative value) except that the first
|
jaroslav@67
|
111 |
* character may be an ASCII minus sign {@code '-'}
|
jaroslav@67
|
112 |
* (<code>'\u002D'</code>) to indicate a negative value or an
|
jaroslav@67
|
113 |
* ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
|
jaroslav@67
|
114 |
* indicate a positive value. The resulting {@code byte} value is
|
jaroslav@67
|
115 |
* returned.
|
jaroslav@67
|
116 |
*
|
jaroslav@67
|
117 |
* <p>An exception of type {@code NumberFormatException} is
|
jaroslav@67
|
118 |
* thrown if any of the following situations occurs:
|
jaroslav@67
|
119 |
* <ul>
|
jaroslav@67
|
120 |
* <li> The first argument is {@code null} or is a string of
|
jaroslav@67
|
121 |
* length zero.
|
jaroslav@67
|
122 |
*
|
jaroslav@67
|
123 |
* <li> The radix is either smaller than {@link
|
jaroslav@67
|
124 |
* java.lang.Character#MIN_RADIX} or larger than {@link
|
jaroslav@67
|
125 |
* java.lang.Character#MAX_RADIX}.
|
jaroslav@67
|
126 |
*
|
jaroslav@67
|
127 |
* <li> Any character of the string is not a digit of the
|
jaroslav@67
|
128 |
* specified radix, except that the first character may be a minus
|
jaroslav@67
|
129 |
* sign {@code '-'} (<code>'\u002D'</code>) or plus sign
|
jaroslav@67
|
130 |
* {@code '+'} (<code>'\u002B'</code>) provided that the
|
jaroslav@67
|
131 |
* string is longer than length 1.
|
jaroslav@67
|
132 |
*
|
jaroslav@67
|
133 |
* <li> The value represented by the string is not a value of type
|
jaroslav@67
|
134 |
* {@code byte}.
|
jaroslav@67
|
135 |
* </ul>
|
jaroslav@67
|
136 |
*
|
jaroslav@67
|
137 |
* @param s the {@code String} containing the
|
jaroslav@67
|
138 |
* {@code byte}
|
jaroslav@67
|
139 |
* representation to be parsed
|
jaroslav@67
|
140 |
* @param radix the radix to be used while parsing {@code s}
|
jaroslav@67
|
141 |
* @return the {@code byte} value represented by the string
|
jaroslav@67
|
142 |
* argument in the specified radix
|
jaroslav@67
|
143 |
* @throws NumberFormatException If the string does
|
jaroslav@67
|
144 |
* not contain a parsable {@code byte}.
|
jaroslav@67
|
145 |
*/
|
jaroslav@67
|
146 |
public static byte parseByte(String s, int radix)
|
jaroslav@67
|
147 |
throws NumberFormatException {
|
jaroslav@67
|
148 |
int i = Integer.parseInt(s, radix);
|
jaroslav@67
|
149 |
if (i < MIN_VALUE || i > MAX_VALUE)
|
jaroslav@67
|
150 |
throw new NumberFormatException(
|
jaroslav@67
|
151 |
"Value out of range. Value:\"" + s + "\" Radix:" + radix);
|
jaroslav@67
|
152 |
return (byte)i;
|
jaroslav@67
|
153 |
}
|
jaroslav@67
|
154 |
|
jaroslav@67
|
155 |
/**
|
jaroslav@67
|
156 |
* Parses the string argument as a signed decimal {@code
|
jaroslav@67
|
157 |
* byte}. The characters in the string must all be decimal digits,
|
jaroslav@67
|
158 |
* except that the first character may be an ASCII minus sign
|
jaroslav@67
|
159 |
* {@code '-'} (<code>'\u002D'</code>) to indicate a negative
|
jaroslav@67
|
160 |
* value or an ASCII plus sign {@code '+'}
|
jaroslav@67
|
161 |
* (<code>'\u002B'</code>) to indicate a positive value. The
|
jaroslav@67
|
162 |
* resulting {@code byte} value is returned, exactly as if the
|
jaroslav@67
|
163 |
* argument and the radix 10 were given as arguments to the {@link
|
jaroslav@67
|
164 |
* #parseByte(java.lang.String, int)} method.
|
jaroslav@67
|
165 |
*
|
jaroslav@67
|
166 |
* @param s a {@code String} containing the
|
jaroslav@67
|
167 |
* {@code byte} representation to be parsed
|
jaroslav@67
|
168 |
* @return the {@code byte} value represented by the
|
jaroslav@67
|
169 |
* argument in decimal
|
jaroslav@67
|
170 |
* @throws NumberFormatException if the string does not
|
jaroslav@67
|
171 |
* contain a parsable {@code byte}.
|
jaroslav@67
|
172 |
*/
|
jaroslav@67
|
173 |
public static byte parseByte(String s) throws NumberFormatException {
|
jaroslav@67
|
174 |
return parseByte(s, 10);
|
jaroslav@67
|
175 |
}
|
jaroslav@67
|
176 |
|
jaroslav@67
|
177 |
/**
|
jaroslav@67
|
178 |
* Returns a {@code Byte} object holding the value
|
jaroslav@67
|
179 |
* extracted from the specified {@code String} when parsed
|
jaroslav@67
|
180 |
* with the radix given by the second argument. The first argument
|
jaroslav@67
|
181 |
* is interpreted as representing a signed {@code byte} in
|
jaroslav@67
|
182 |
* the radix specified by the second argument, exactly as if the
|
jaroslav@67
|
183 |
* argument were given to the {@link #parseByte(java.lang.String,
|
jaroslav@67
|
184 |
* int)} method. The result is a {@code Byte} object that
|
jaroslav@67
|
185 |
* represents the {@code byte} value specified by the string.
|
jaroslav@67
|
186 |
*
|
jaroslav@67
|
187 |
* <p> In other words, this method returns a {@code Byte} object
|
jaroslav@67
|
188 |
* equal to the value of:
|
jaroslav@67
|
189 |
*
|
jaroslav@67
|
190 |
* <blockquote>
|
jaroslav@67
|
191 |
* {@code new Byte(Byte.parseByte(s, radix))}
|
jaroslav@67
|
192 |
* </blockquote>
|
jaroslav@67
|
193 |
*
|
jaroslav@67
|
194 |
* @param s the string to be parsed
|
jaroslav@67
|
195 |
* @param radix the radix to be used in interpreting {@code s}
|
jaroslav@67
|
196 |
* @return a {@code Byte} object holding the value
|
jaroslav@67
|
197 |
* represented by the string argument in the
|
jaroslav@67
|
198 |
* specified radix.
|
jaroslav@67
|
199 |
* @throws NumberFormatException If the {@code String} does
|
jaroslav@67
|
200 |
* not contain a parsable {@code byte}.
|
jaroslav@67
|
201 |
*/
|
jaroslav@67
|
202 |
public static Byte valueOf(String s, int radix)
|
jaroslav@67
|
203 |
throws NumberFormatException {
|
jaroslav@67
|
204 |
return valueOf(parseByte(s, radix));
|
jaroslav@67
|
205 |
}
|
jaroslav@67
|
206 |
|
jaroslav@67
|
207 |
/**
|
jaroslav@67
|
208 |
* Returns a {@code Byte} object holding the value
|
jaroslav@67
|
209 |
* given by the specified {@code String}. The argument is
|
jaroslav@67
|
210 |
* interpreted as representing a signed decimal {@code byte},
|
jaroslav@67
|
211 |
* exactly as if the argument were given to the {@link
|
jaroslav@67
|
212 |
* #parseByte(java.lang.String)} method. The result is a
|
jaroslav@67
|
213 |
* {@code Byte} object that represents the {@code byte}
|
jaroslav@67
|
214 |
* value specified by the string.
|
jaroslav@67
|
215 |
*
|
jaroslav@67
|
216 |
* <p> In other words, this method returns a {@code Byte} object
|
jaroslav@67
|
217 |
* equal to the value of:
|
jaroslav@67
|
218 |
*
|
jaroslav@67
|
219 |
* <blockquote>
|
jaroslav@67
|
220 |
* {@code new Byte(Byte.parseByte(s))}
|
jaroslav@67
|
221 |
* </blockquote>
|
jaroslav@67
|
222 |
*
|
jaroslav@67
|
223 |
* @param s the string to be parsed
|
jaroslav@67
|
224 |
* @return a {@code Byte} object holding the value
|
jaroslav@67
|
225 |
* represented by the string argument
|
jaroslav@67
|
226 |
* @throws NumberFormatException If the {@code String} does
|
jaroslav@67
|
227 |
* not contain a parsable {@code byte}.
|
jaroslav@67
|
228 |
*/
|
jaroslav@67
|
229 |
public static Byte valueOf(String s) throws NumberFormatException {
|
jaroslav@67
|
230 |
return valueOf(s, 10);
|
jaroslav@67
|
231 |
}
|
jaroslav@67
|
232 |
|
jaroslav@67
|
233 |
/**
|
jaroslav@67
|
234 |
* Decodes a {@code String} into a {@code Byte}.
|
jaroslav@67
|
235 |
* Accepts decimal, hexadecimal, and octal numbers given by
|
jaroslav@67
|
236 |
* the following grammar:
|
jaroslav@67
|
237 |
*
|
jaroslav@67
|
238 |
* <blockquote>
|
jaroslav@67
|
239 |
* <dl>
|
jaroslav@67
|
240 |
* <dt><i>DecodableString:</i>
|
jaroslav@67
|
241 |
* <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
|
jaroslav@67
|
242 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
|
jaroslav@67
|
243 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
|
jaroslav@67
|
244 |
* <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
|
jaroslav@67
|
245 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
|
jaroslav@67
|
246 |
* <p>
|
jaroslav@67
|
247 |
* <dt><i>Sign:</i>
|
jaroslav@67
|
248 |
* <dd>{@code -}
|
jaroslav@67
|
249 |
* <dd>{@code +}
|
jaroslav@67
|
250 |
* </dl>
|
jaroslav@67
|
251 |
* </blockquote>
|
jaroslav@67
|
252 |
*
|
jaroslav@67
|
253 |
* <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
|
jaroslav@67
|
254 |
* are as defined in section 3.10.1 of
|
jaroslav@67
|
255 |
* <cite>The Java™ Language Specification</cite>,
|
jaroslav@67
|
256 |
* except that underscores are not accepted between digits.
|
jaroslav@67
|
257 |
*
|
jaroslav@67
|
258 |
* <p>The sequence of characters following an optional
|
jaroslav@67
|
259 |
* sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
|
jaroslav@67
|
260 |
* "{@code #}", or leading zero) is parsed as by the {@code
|
jaroslav@67
|
261 |
* Byte.parseByte} method with the indicated radix (10, 16, or 8).
|
jaroslav@67
|
262 |
* This sequence of characters must represent a positive value or
|
jaroslav@67
|
263 |
* a {@link NumberFormatException} will be thrown. The result is
|
jaroslav@67
|
264 |
* negated if first character of the specified {@code String} is
|
jaroslav@67
|
265 |
* the minus sign. No whitespace characters are permitted in the
|
jaroslav@67
|
266 |
* {@code String}.
|
jaroslav@67
|
267 |
*
|
jaroslav@67
|
268 |
* @param nm the {@code String} to decode.
|
jaroslav@67
|
269 |
* @return a {@code Byte} object holding the {@code byte}
|
jaroslav@67
|
270 |
* value represented by {@code nm}
|
jaroslav@67
|
271 |
* @throws NumberFormatException if the {@code String} does not
|
jaroslav@67
|
272 |
* contain a parsable {@code byte}.
|
jaroslav@67
|
273 |
* @see java.lang.Byte#parseByte(java.lang.String, int)
|
jaroslav@67
|
274 |
*/
|
jaroslav@67
|
275 |
public static Byte decode(String nm) throws NumberFormatException {
|
jaroslav@67
|
276 |
int i = Integer.decode(nm);
|
jaroslav@67
|
277 |
if (i < MIN_VALUE || i > MAX_VALUE)
|
jaroslav@67
|
278 |
throw new NumberFormatException(
|
jaroslav@67
|
279 |
"Value " + i + " out of range from input " + nm);
|
jaroslav@67
|
280 |
return valueOf((byte)i);
|
jaroslav@67
|
281 |
}
|
jaroslav@67
|
282 |
|
jaroslav@67
|
283 |
/**
|
jaroslav@67
|
284 |
* The value of the {@code Byte}.
|
jaroslav@67
|
285 |
*
|
jaroslav@67
|
286 |
* @serial
|
jaroslav@67
|
287 |
*/
|
jaroslav@67
|
288 |
private final byte value;
|
jaroslav@67
|
289 |
|
jaroslav@67
|
290 |
/**
|
jaroslav@67
|
291 |
* Constructs a newly allocated {@code Byte} object that
|
jaroslav@67
|
292 |
* represents the specified {@code byte} value.
|
jaroslav@67
|
293 |
*
|
jaroslav@67
|
294 |
* @param value the value to be represented by the
|
jaroslav@67
|
295 |
* {@code Byte}.
|
jaroslav@67
|
296 |
*/
|
jaroslav@67
|
297 |
public Byte(byte value) {
|
jaroslav@67
|
298 |
this.value = value;
|
jaroslav@67
|
299 |
}
|
jaroslav@67
|
300 |
|
jaroslav@67
|
301 |
/**
|
jaroslav@67
|
302 |
* Constructs a newly allocated {@code Byte} object that
|
jaroslav@67
|
303 |
* represents the {@code byte} value indicated by the
|
jaroslav@67
|
304 |
* {@code String} parameter. The string is converted to a
|
jaroslav@67
|
305 |
* {@code byte} value in exactly the manner used by the
|
jaroslav@67
|
306 |
* {@code parseByte} method for radix 10.
|
jaroslav@67
|
307 |
*
|
jaroslav@67
|
308 |
* @param s the {@code String} to be converted to a
|
jaroslav@67
|
309 |
* {@code Byte}
|
jaroslav@67
|
310 |
* @throws NumberFormatException If the {@code String}
|
jaroslav@67
|
311 |
* does not contain a parsable {@code byte}.
|
jaroslav@67
|
312 |
* @see java.lang.Byte#parseByte(java.lang.String, int)
|
jaroslav@67
|
313 |
*/
|
jaroslav@67
|
314 |
public Byte(String s) throws NumberFormatException {
|
jaroslav@67
|
315 |
this.value = parseByte(s, 10);
|
jaroslav@67
|
316 |
}
|
jaroslav@67
|
317 |
|
jaroslav@67
|
318 |
/**
|
jaroslav@67
|
319 |
* Returns the value of this {@code Byte} as a
|
jaroslav@67
|
320 |
* {@code byte}.
|
jaroslav@67
|
321 |
*/
|
jaroslav@67
|
322 |
public byte byteValue() {
|
jaroslav@67
|
323 |
return value;
|
jaroslav@67
|
324 |
}
|
jaroslav@67
|
325 |
|
jaroslav@67
|
326 |
/**
|
jaroslav@67
|
327 |
* Returns the value of this {@code Byte} as a
|
jaroslav@67
|
328 |
* {@code short}.
|
jaroslav@67
|
329 |
*/
|
jaroslav@67
|
330 |
public short shortValue() {
|
jaroslav@67
|
331 |
return (short)value;
|
jaroslav@67
|
332 |
}
|
jaroslav@67
|
333 |
|
jaroslav@67
|
334 |
/**
|
jaroslav@67
|
335 |
* Returns the value of this {@code Byte} as an
|
jaroslav@67
|
336 |
* {@code int}.
|
jaroslav@67
|
337 |
*/
|
jaroslav@67
|
338 |
public int intValue() {
|
jaroslav@67
|
339 |
return (int)value;
|
jaroslav@67
|
340 |
}
|
jaroslav@67
|
341 |
|
jaroslav@67
|
342 |
/**
|
jaroslav@67
|
343 |
* Returns the value of this {@code Byte} as a
|
jaroslav@67
|
344 |
* {@code long}.
|
jaroslav@67
|
345 |
*/
|
jaroslav@67
|
346 |
public long longValue() {
|
jaroslav@67
|
347 |
return (long)value;
|
jaroslav@67
|
348 |
}
|
jaroslav@67
|
349 |
|
jaroslav@67
|
350 |
/**
|
jaroslav@67
|
351 |
* Returns the value of this {@code Byte} as a
|
jaroslav@67
|
352 |
* {@code float}.
|
jaroslav@67
|
353 |
*/
|
jaroslav@67
|
354 |
public float floatValue() {
|
jaroslav@67
|
355 |
return (float)value;
|
jaroslav@67
|
356 |
}
|
jaroslav@67
|
357 |
|
jaroslav@67
|
358 |
/**
|
jaroslav@67
|
359 |
* Returns the value of this {@code Byte} as a
|
jaroslav@67
|
360 |
* {@code double}.
|
jaroslav@67
|
361 |
*/
|
jaroslav@67
|
362 |
public double doubleValue() {
|
jaroslav@67
|
363 |
return (double)value;
|
jaroslav@67
|
364 |
}
|
jaroslav@67
|
365 |
|
jaroslav@67
|
366 |
/**
|
jaroslav@67
|
367 |
* Returns a {@code String} object representing this
|
jaroslav@67
|
368 |
* {@code Byte}'s value. The value is converted to signed
|
jaroslav@67
|
369 |
* decimal representation and returned as a string, exactly as if
|
jaroslav@67
|
370 |
* the {@code byte} value were given as an argument to the
|
jaroslav@67
|
371 |
* {@link java.lang.Byte#toString(byte)} method.
|
jaroslav@67
|
372 |
*
|
jaroslav@67
|
373 |
* @return a string representation of the value of this object in
|
jaroslav@67
|
374 |
* base 10.
|
jaroslav@67
|
375 |
*/
|
jaroslav@67
|
376 |
public String toString() {
|
jaroslav@67
|
377 |
return Integer.toString((int)value);
|
jaroslav@67
|
378 |
}
|
jaroslav@67
|
379 |
|
jaroslav@67
|
380 |
/**
|
jaroslav@67
|
381 |
* Returns a hash code for this {@code Byte}; equal to the result
|
jaroslav@67
|
382 |
* of invoking {@code intValue()}.
|
jaroslav@67
|
383 |
*
|
jaroslav@67
|
384 |
* @return a hash code value for this {@code Byte}
|
jaroslav@67
|
385 |
*/
|
jaroslav@67
|
386 |
public int hashCode() {
|
jaroslav@67
|
387 |
return (int)value;
|
jaroslav@67
|
388 |
}
|
jaroslav@67
|
389 |
|
jaroslav@67
|
390 |
/**
|
jaroslav@67
|
391 |
* Compares this object to the specified object. The result is
|
jaroslav@67
|
392 |
* {@code true} if and only if the argument is not
|
jaroslav@67
|
393 |
* {@code null} and is a {@code Byte} object that
|
jaroslav@67
|
394 |
* contains the same {@code byte} value as this object.
|
jaroslav@67
|
395 |
*
|
jaroslav@67
|
396 |
* @param obj the object to compare with
|
jaroslav@67
|
397 |
* @return {@code true} if the objects are the same;
|
jaroslav@67
|
398 |
* {@code false} otherwise.
|
jaroslav@67
|
399 |
*/
|
jaroslav@67
|
400 |
public boolean equals(Object obj) {
|
jaroslav@67
|
401 |
if (obj instanceof Byte) {
|
jaroslav@67
|
402 |
return value == ((Byte)obj).byteValue();
|
jaroslav@67
|
403 |
}
|
jaroslav@67
|
404 |
return false;
|
jaroslav@67
|
405 |
}
|
jaroslav@67
|
406 |
|
jaroslav@67
|
407 |
/**
|
jaroslav@67
|
408 |
* Compares two {@code Byte} objects numerically.
|
jaroslav@67
|
409 |
*
|
jaroslav@67
|
410 |
* @param anotherByte the {@code Byte} to be compared.
|
jaroslav@67
|
411 |
* @return the value {@code 0} if this {@code Byte} is
|
jaroslav@67
|
412 |
* equal to the argument {@code Byte}; a value less than
|
jaroslav@67
|
413 |
* {@code 0} if this {@code Byte} is numerically less
|
jaroslav@67
|
414 |
* than the argument {@code Byte}; and a value greater than
|
jaroslav@67
|
415 |
* {@code 0} if this {@code Byte} is numerically
|
jaroslav@67
|
416 |
* greater than the argument {@code Byte} (signed
|
jaroslav@67
|
417 |
* comparison).
|
jaroslav@67
|
418 |
* @since 1.2
|
jaroslav@67
|
419 |
*/
|
jaroslav@67
|
420 |
public int compareTo(Byte anotherByte) {
|
jaroslav@67
|
421 |
return compare(this.value, anotherByte.value);
|
jaroslav@67
|
422 |
}
|
jaroslav@67
|
423 |
|
jaroslav@67
|
424 |
/**
|
jaroslav@67
|
425 |
* Compares two {@code byte} values numerically.
|
jaroslav@67
|
426 |
* The value returned is identical to what would be returned by:
|
jaroslav@67
|
427 |
* <pre>
|
jaroslav@67
|
428 |
* Byte.valueOf(x).compareTo(Byte.valueOf(y))
|
jaroslav@67
|
429 |
* </pre>
|
jaroslav@67
|
430 |
*
|
jaroslav@67
|
431 |
* @param x the first {@code byte} to compare
|
jaroslav@67
|
432 |
* @param y the second {@code byte} to compare
|
jaroslav@67
|
433 |
* @return the value {@code 0} if {@code x == y};
|
jaroslav@67
|
434 |
* a value less than {@code 0} if {@code x < y}; and
|
jaroslav@67
|
435 |
* a value greater than {@code 0} if {@code x > y}
|
jaroslav@67
|
436 |
* @since 1.7
|
jaroslav@67
|
437 |
*/
|
jaroslav@67
|
438 |
public static int compare(byte x, byte y) {
|
jaroslav@67
|
439 |
return x - y;
|
jaroslav@67
|
440 |
}
|
jaroslav@67
|
441 |
|
jaroslav@67
|
442 |
/**
|
jaroslav@67
|
443 |
* The number of bits used to represent a {@code byte} value in two's
|
jaroslav@67
|
444 |
* complement binary form.
|
jaroslav@67
|
445 |
*
|
jaroslav@67
|
446 |
* @since 1.5
|
jaroslav@67
|
447 |
*/
|
jaroslav@67
|
448 |
public static final int SIZE = 8;
|
jaroslav@67
|
449 |
|
jaroslav@67
|
450 |
/** use serialVersionUID from JDK 1.1. for interoperability */
|
jaroslav@67
|
451 |
private static final long serialVersionUID = -7183698231559129828L;
|
jaroslav@67
|
452 |
}
|