1 /* Byte.java -- object wrapper for byte
2 Copyright (C) 1998, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
42 * Instances of class <code>Byte</code> represent primitive <code>byte</code>
45 * Additionally, this class provides various helper functions and variables
51 * @author Eric Blake (ebb9@email.byu.edu)
52 * @author Tom Tromey (tromey@redhat.com)
53 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
55 * @status updated to 1.5
57 public final class Byte
extends Number
implements Comparable
<Byte
>
60 * Compatible with JDK 1.1+.
62 private static final long serialVersionUID
= -7183698231559129828L;
65 * The minimum value a <code>byte</code> can represent is -128 (or
68 public static final byte MIN_VALUE
= -128;
71 * The maximum value a <code>byte</code> can represent is 127 (or
74 public static final byte MAX_VALUE
= 127;
77 * The primitive type <code>byte</code> is represented by this
78 * <code>Class</code> object.
80 public static final Class
<Byte
> TYPE
= (Class
<Byte
>) VMClassLoader
.getPrimitiveClass('B');
83 * The number of bits needed to represent a <code>byte</code>.
86 public static final int SIZE
= 8;
88 // This caches Byte values, and is used by boxing conversions via
89 // valueOf(). We're required to cache all possible values here.
90 private static Byte
[] byteCache
= new Byte
[MAX_VALUE
- MIN_VALUE
+ 1];
94 * The immutable value of this Byte.
96 * @serial the wrapped byte
98 private final byte value
;
101 * Create a <code>Byte</code> object representing the value of the
102 * <code>byte</code> argument.
104 * @param value the value to use
106 public Byte(byte value
)
112 * Create a <code>Byte</code> object representing the value specified
113 * by the <code>String</code> argument
115 * @param s the string to convert
116 * @throws NumberFormatException if the String does not contain a byte
117 * @see #valueOf(String)
119 public Byte(String s
)
121 value
= parseByte(s
, 10);
125 * Converts the <code>byte</code> to a <code>String</code> and assumes
128 * @param b the <code>byte</code> to convert to <code>String</code>
129 * @return the <code>String</code> representation of the argument
131 public static String
toString(byte b
)
133 return String
.valueOf(b
);
137 * Converts the specified <code>String</code> into a <code>byte</code>.
138 * This function assumes a radix of 10.
140 * @param s the <code>String</code> to convert
141 * @return the <code>byte</code> value of <code>s</code>
142 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
144 * @see #parseByte(String)
146 public static byte parseByte(String s
)
148 return parseByte(s
, 10);
152 * Converts the specified <code>String</code> into an <code>int</code>
153 * using the specified radix (base). The string must not be <code>null</code>
154 * or empty. It may begin with an optional '-', which will negate the answer,
155 * provided that there are also valid digits. Each digit is parsed as if by
156 * <code>Character.digit(d, radix)</code>, and must be in the range
157 * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
158 * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
159 * Unlike Double.parseDouble, you may not have a leading '+'.
161 * @param s the <code>String</code> to convert
162 * @param radix the radix (base) to use in the conversion
163 * @return the <code>String</code> argument converted to <code>byte</code>
164 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
167 public static byte parseByte(String s
, int radix
)
169 int i
= Integer
.parseInt(s
, radix
, false);
171 throw new NumberFormatException();
176 * Creates a new <code>Byte</code> object using the <code>String</code>
177 * and specified radix (base).
179 * @param s the <code>String</code> to convert
180 * @param radix the radix (base) to convert with
181 * @return the new <code>Byte</code>
182 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
184 * @see #parseByte(String, int)
186 public static Byte
valueOf(String s
, int radix
)
188 return new Byte(parseByte(s
, radix
));
192 * Creates a new <code>Byte</code> object using the <code>String</code>,
193 * assuming a radix of 10.
195 * @param s the <code>String</code> to convert
196 * @return the new <code>Byte</code>
197 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
200 * @see #parseByte(String)
202 public static Byte
valueOf(String s
)
204 return new Byte(parseByte(s
, 10));
208 * Returns a <code>Byte</code> object wrapping the value.
209 * In contrast to the <code>Byte</code> constructor, this method
210 * will cache some values. It is used by boxing conversion.
212 * @param val the value to wrap
213 * @return the <code>Byte</code>
215 public static Byte
valueOf(byte val
)
217 synchronized (byteCache
)
219 if (byteCache
[val
- MIN_VALUE
] == null)
220 byteCache
[val
- MIN_VALUE
] = new Byte(val
);
221 return byteCache
[val
- MIN_VALUE
];
226 * Convert the specified <code>String</code> into a <code>Byte</code>.
227 * The <code>String</code> may represent decimal, hexadecimal, or
230 * <p>The extended BNF grammar is as follows:<br>
232 * <em>DecodableString</em>:
233 * ( [ <code>-</code> ] <em>DecimalNumber</em> )
234 * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
235 * | <code>#</code> ) { <em>HexDigit</em> }+ )
236 * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
237 * <em>DecimalNumber</em>:
238 * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
239 * <em>DecimalDigit</em>:
240 * <em>Character.digit(d, 10) has value 0 to 9</em>
241 * <em>OctalDigit</em>:
242 * <em>Character.digit(d, 8) has value 0 to 7</em>
243 * <em>DecimalDigit</em>:
244 * <em>Character.digit(d, 16) has value 0 to 15</em>
246 * Finally, the value must be in the range <code>MIN_VALUE</code> to
247 * <code>MAX_VALUE</code>, or an exception is thrown.
249 * @param s the <code>String</code> to interpret
250 * @return the value of the String as a <code>Byte</code>
251 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
253 * @throws NullPointerException if <code>s</code> is null
254 * @see Integer#decode(String)
256 public static Byte
decode(String s
)
258 int i
= Integer
.parseInt(s
, 10, true);
260 throw new NumberFormatException();
261 return new Byte((byte) i
);
265 * Return the value of this <code>Byte</code>.
267 * @return the byte value
269 public byte byteValue()
275 * Return the value of this <code>Byte</code> as a <code>short</code>.
277 * @return the short value
279 public short shortValue()
285 * Return the value of this <code>Byte</code> as an <code>int</code>.
287 * @return the int value
289 public int intValue()
295 * Return the value of this <code>Byte</code> as a <code>long</code>.
297 * @return the long value
299 public long longValue()
305 * Return the value of this <code>Byte</code> as a <code>float</code>.
307 * @return the float value
309 public float floatValue()
315 * Return the value of this <code>Byte</code> as a <code>double</code>.
317 * @return the double value
319 public double doubleValue()
325 * Converts the <code>Byte</code> value to a <code>String</code> and
326 * assumes a radix of 10.
328 * @return the <code>String</code> representation of this <code>Byte</code>
329 * @see Integer#toString()
331 public String
toString()
333 return String
.valueOf(value
);
337 * Return a hashcode representing this Object. <code>Byte</code>'s hash
338 * code is simply its value.
340 * @return this Object's hash code
342 public int hashCode()
348 * Returns <code>true</code> if <code>obj</code> is an instance of
349 * <code>Byte</code> and represents the same byte value.
351 * @param obj the object to compare
352 * @return whether these Objects are semantically equal
354 public boolean equals(Object obj
)
356 return obj
instanceof Byte
&& value
== ((Byte
) obj
).value
;
360 * Compare two Bytes numerically by comparing their <code>byte</code> values.
361 * The result is positive if the first is greater, negative if the second
362 * is greater, and 0 if the two are equal.
364 * @param b the Byte to compare
365 * @return the comparison
368 public int compareTo(Byte b
)
370 return value
- b
.value
;