1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package java.lang;
/**
* The wrapper for the primitive type {@code byte}.
*
* @since 1.1
*/
@FindBugsSuppressWarnings("DM_NUMBER_CTOR")
public final class Byte extends Number implements Comparable<Byte> {
private static final long serialVersionUID = -7183698231559129828L;
/**
* The value which the receiver represents.
*/
private final byte value;
/**
* The maximum {@code Byte} value, 2<sup>7</sup>-1.
*/
public static final byte MAX_VALUE = (byte) 0x7F;
/**
* The minimum {@code Byte} value, -2<sup>7</sup>.
*/
public static final byte MIN_VALUE = (byte) 0x80;
/**
* The number of bits needed to represent a {@code Byte} value in two's
* complement form.
*
* @since 1.5
*/
public static final int SIZE = 8;
/**
* The {@link Class} object that represents the primitive type {@code byte}.
*/
@SuppressWarnings("unchecked")
public static final Class<Byte> TYPE
= (Class<Byte>) byte[].class.getComponentType();
// Note: Byte.TYPE can't be set to "byte.class", since *that* is
// defined to be "java.lang.Byte.TYPE";
/**
* Constructs a new {@code Byte} with the specified primitive byte value.
*
* @param value
* the primitive byte value to store in the new instance.
*/
public Byte(byte value) {
this.value = value;
}
/**
* Constructs a new {@code Byte} from the specified string.
*
* @param string
* the string representation of a single byte value.
* @throws NumberFormatException
* if {@code string} cannot be parsed as a byte value.
* @see #parseByte(String)
*/
public Byte(String string) throws NumberFormatException {
this(parseByte(string));
}
/**
* Gets the primitive value of this byte.
*
* @return this object's primitive value.
*/
@Override
public byte byteValue() {
return value;
}
/**
* Compares this object to the specified byte object to determine their
* relative order.
*
* @param object
* the byte object to compare this object to.
* @return a negative value if the value of this byte is less than the value
* of {@code object}; 0 if the value of this byte and the value of
* {@code object} are equal; a positive value if the value of this
* byte is greater than the value of {@code object}.
* @see java.lang.Comparable
* @since 1.2
*/
public int compareTo(Byte object) {
return compare(value, object.value);
}
/**
* Compares two {@code byte} values.
* @return 0 if lhs = rhs, less than 0 if lhs < rhs, and greater than 0 if lhs > rhs.
* @since 1.7
*/
public static int compare(byte lhs, byte rhs) {
return lhs > rhs ? 1 : (lhs < rhs ? -1 : 0);
}
/**
* Parses the specified string and returns a {@code Byte} instance if the
* string can be decoded into a single byte value. The string may be an
* optional minus sign "-" followed by a hexadecimal ("0x..." or "#..."),
* octal ("0..."), or decimal ("...") representation of a byte.
*
* @param string
* a string representation of a single byte value.
* @return a {@code Byte} containing the value represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as a byte value.
*/
public static Byte decode(String string) throws NumberFormatException {
int intValue = Integer.decode(string);
byte result = (byte) intValue;
if (result == intValue) {
return valueOf(result);
}
throw new NumberFormatException("Value out of range for byte: \"" + string + "\"");
}
@Override
public double doubleValue() {
return value;
}
/**
* Compares this object with the specified object and indicates if they are
* equal. In order to be equal, {@code object} must be an instance of
* {@code Byte} and have the same byte value as this object.
*
* @param object
* the object to compare this byte with.
* @return {@code true} if the specified object is equal to this
* {@code Byte}; {@code false} otherwise.
*/
@Override
@FindBugsSuppressWarnings("RC_REF_COMPARISON")
public boolean equals(Object object) {
return (object == this) || ((object instanceof Byte) && (((Byte) object).value == value));
}
@Override
public float floatValue() {
return value;
}
@Override
public int hashCode() {
return value;
}
@Override
public int intValue() {
return value;
}
@Override
public long longValue() {
return value;
}
/**
* Parses the specified string as a signed decimal byte value. The ASCII
* character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of a single byte value.
* @return the primitive byte value represented by {@code string}.
* @throws NumberFormatException
* if {@code string} can not be parsed as a byte value.
*/
public static byte parseByte(String string) throws NumberFormatException {
return parseByte(string, 10);
}
/**
* Parses the specified string as a signed byte value using the specified
* radix. The ASCII character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of a single byte value.
* @param radix
* the radix to use when parsing.
* @return the primitive byte value represented by {@code string} using
* {@code radix}.
* @throws NumberFormatException
* if {@code string} can not be parsed as a byte value, or
* {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
*/
public static byte parseByte(String string, int radix) throws NumberFormatException {
int intValue = Integer.parseInt(string, radix);
byte result = (byte) intValue;
if (result == intValue) {
return result;
}
throw new NumberFormatException("Value out of range for byte: \"" + string + "\"");
}
@Override
public short shortValue() {
return value;
}
@Override
public String toString() {
return Integer.toString(value);
}
/**
* Returns a two-digit hex string. That is, -1 becomes "ff" or "FF" and 2 becomes "02".
* @hide internal use only
*/
public static String toHexString(byte b, boolean upperCase) {
return IntegralToString.byteToHexString(b, upperCase);
}
/**
* Returns a string containing a concise, human-readable description of the
* specified byte value.
*
* @param value
* the byte to convert to a string.
* @return a printable representation of {@code value}.
*/
public static String toString(byte value) {
return Integer.toString(value);
}
/**
* Parses the specified string as a signed decimal byte value.
*
* @param string
* the string representation of a single byte value.
* @return a {@code Byte} instance containing the byte value represented by
* {@code string}.
* @throws NumberFormatException
* if {@code string} can not be parsed as a byte value.
* @see #parseByte(String)
*/
public static Byte valueOf(String string) throws NumberFormatException {
return valueOf(parseByte(string));
}
/**
* Parses the specified string as a signed byte value using the specified
* radix.
*
* @param string
* the string representation of a single byte value.
* @param radix
* the radix to use when parsing.
* @return a {@code Byte} instance containing the byte value represented by
* {@code string} using {@code radix}.
* @throws NumberFormatException
* if {@code string} can not be parsed as a byte value, or
* {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
* @see #parseByte(String, int)
*/
public static Byte valueOf(String string, int radix) throws NumberFormatException {
return valueOf(parseByte(string, radix));
}
/**
* Returns a {@code Byte} instance for the specified byte value.
* <p>
* If it is not necessary to get a new {@code Byte} instance, it is
* recommended to use this method instead of the constructor, since it
* maintains a cache of instances which may result in better performance.
*
* @param b
* the byte value to store in the instance.
* @return a {@code Byte} instance containing {@code b}.
* @since 1.5
*/
public static Byte valueOf(byte b) {
return VALUES[b + 128];
}
/**
* A cache of instances used by {@link Byte#valueOf(byte)} and auto-boxing
*/
private static final Byte[] VALUES = new Byte[256];
static {
for (int i = -128; i < 128; i++) {
VALUES[i + 128] = new Byte((byte) i);
}
}
}
|