source: trunk/src/gcc/libjava/java/lang/Byte.java@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 9.6 KB
Line 
1/* Byte.java -- object wrapper for byte
2 Copyright (C) 1998, 2001 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.lang;
40
41/**
42 * Instances of class <code>Byte</code> represent primitive <code>byte</code>
43 * values.
44 *
45 * Additionally, this class provides various helper functions and variables
46 * useful to bytes.
47 *
48 * @author Paul Fisher
49 * @author John Keiser
50 * @author Per Bothner
51 * @since JDK 1.0
52 */
53public final class Byte extends Number implements Comparable
54{
55 static final long serialVersionUID = -7183698231559129828L;
56
57 /**
58 * The minimum value a <code>byte</code> can represent is -128.
59 */
60 public static final byte MIN_VALUE = -128;
61
62 /**
63 * The maximum value a <code>byte</code> can represent is 127.
64 */
65 public static final byte MAX_VALUE = 127;
66
67 /**
68 * The primitive type <code>byte</code> is represented by this
69 * <code>Class</code> object.
70 */
71 public static final Class TYPE = VMClassLoader.getPrimitiveClass('B');
72
73 /**
74 * The immutable value of this Byte.
75 */
76 private final byte value;
77
78 /**
79 * Create a <code>Byte</code> object representing the value of the
80 * <code>byte</code> argument.
81 *
82 * @param value the value to use
83 */
84 public Byte(byte value)
85 {
86 this.value = value;
87 }
88
89 /**
90 * Create a <code>Byte</code> object representing the value specified
91 * by the <code>String</code> argument.
92 *
93 * @param s the string to convert.
94 */
95 public Byte(String s) throws NumberFormatException
96 {
97 value = parseByte(s, 10);
98 }
99
100 /**
101 * Return a hashcode representing this Object.
102 *
103 * <code>Byte</code>'s hash code is calculated by simply returning its
104 * value.
105 *
106 * @return this Object's hash code.
107 */
108 public int hashCode()
109 {
110 return value;
111 }
112
113 /**
114 * Returns <code>true</code> if <code>obj</code> is an instance of
115 * <code>Byte</code> and represents the same byte value.
116 * @return whether these Objects are semantically equal.
117 */
118 public boolean equals(Object obj)
119 {
120 return ((obj instanceof Byte) && (value == ((Byte)obj).byteValue()));
121 }
122
123 /**
124 * Converts the <code>byte</code> to a <code>String</code> and assumes
125 * a radix of 10.
126 * @param i the <code>byte</code> to convert to <code>String</code>
127 * @return the <code>String</code> representation of the argument.
128 */
129 public static String toString(byte i)
130 {
131 return Integer.toString ((int) i);
132 }
133
134 /**
135 * Converts the <code>Byte</code> value to a <code>String</code> and
136 * assumes a radix of 10.
137 * @return the <code>String</code> representation of this <code>Byte</code>.
138 */
139 public String toString()
140 {
141 return Integer.toString ((int) value);
142 }
143
144 /**
145 * Creates a new <code>Byte</code> object using the <code>String</code>,
146 * assuming a radix of 10.
147 * @param s the <code>String</code> to convert.
148 * @return the new <code>Byte</code>.
149 * @see #Byte(java.lang.String)
150 * @see #parseByte(java.lang.String)
151 * @exception NumberFormatException thrown if the <code>String</code>
152 * cannot be parsed as a <code>byte</code>.
153 */
154 public static Byte valueOf(String s) throws NumberFormatException
155 {
156 return new Byte(parseByte(s));
157 }
158
159 /**
160 * Creates a new <code>Byte</code> object using the <code>String</code>
161 * and specified radix (base).
162 * @param s the <code>String</code> to convert.
163 * @param radix the radix (base) to convert with.
164 * @return the new <code>Byte</code>.
165 * @see #parseByte(java.lang.String,int)
166 * @exception NumberFormatException thrown if the <code>String</code>
167 * cannot be parsed as a <code>byte</code>.
168 */
169 public static Byte valueOf(String s, int radix)
170 throws NumberFormatException
171 {
172 return new Byte(parseByte(s, radix));
173 }
174
175 /**
176 * Converts the specified <code>String</code> into a <code>byte</code>.
177 * This function assumes a radix of 10.
178 *
179 * @param s the <code>String</code> to convert
180 * @return the <code>byte</code> value of the <code>String</code>
181 * argument.
182 * @exception NumberFormatException thrown if the <code>String</code>
183 * cannot be parsed as a <code>byte</code>.
184 */
185 public static byte parseByte(String s) throws NumberFormatException
186 {
187 return parseByte(s, 10);
188 }
189
190 /**
191 * Converts the specified <code>String</code> into a <code>byte</code>
192 * using the specified radix (base).
193 *
194 * @param str the <code>String</code> to convert
195 * @param radix the radix (base) to use in the conversion
196 * @return the <code>String</code> argument converted to </code>byte</code>.
197 * @exception NumberFormatException thrown if the <code>String</code>
198 * cannot be parsed as a <code>byte</code>.
199 */
200 public static byte parseByte(String str, int radix)
201 throws NumberFormatException
202 {
203 int i = Integer.parseInt(str, radix);
204 if (i < MIN_VALUE || i > MAX_VALUE)
205 throw new NumberFormatException();
206 return (byte) i;
207 }
208
209 /**
210 * Convert the specified <code>String</code> into a <code>Byte</code>.
211 * The <code>String</code> may represent decimal, hexadecimal, or
212 * octal numbers.
213 *
214 * The <code>String</code> argument is interpreted based on the leading
215 * characters. Depending on what the String begins with, the base will be
216 * interpreted differently:
217 *
218 * <table>
219 * <tr><th>Leading<br>Characters</th><th>Base</th></tr>
220 * <tr><td>#</td><td>16</td></tr>
221 * <tr><td>0x</td><td>16</td></tr>
222 * <tr><td>0X</td><td>16</td></tr>
223 * <tr><td>0</td><td>8</td></tr>
224 * <tr><td>Anything<br>Else</td><td>10</td></tr>
225 * </table>
226 *
227 * @param str the <code>String</code> to interpret.
228 * @return the value of the String as a <code>Byte</code>.
229 * @exception NumberFormatException thrown if the <code>String</code>
230 * cannot be parsed as a <code>byte</code>.
231 */
232 public static Byte decode(String str) throws NumberFormatException
233 {
234 int i = (Integer.decode(str)).intValue();
235 if (i < MIN_VALUE || i > MAX_VALUE)
236 throw new NumberFormatException();
237 return new Byte((byte) i);
238 }
239
240 /** Return the value of this <code>Byte</code> as an <code>short</code>.
241 ** @return the value of this <code>Byte</code> as an <code>short</code>.
242 **/
243 public byte byteValue()
244 {
245 return value;
246 }
247
248 /** Return the value of this <code>Byte</code> as an <code>short</code>.
249 ** @return the value of this <code>Byte</code> as an <code>short</code>.
250 **/
251 public short shortValue()
252 {
253 return value;
254 }
255
256 /** Return the value of this <code>Byte</code> as an <code>int</code>.
257 ** @return the value of this <code>Byte</code> as an <code>int</code>.
258 **/
259 public int intValue()
260 {
261 return value;
262 }
263
264 /** Return the value of this <code>Byte</code> as a <code>long</code>.
265 ** @return the value of this <code>Byte</code> as a <code>long</code>.
266 **/
267 public long longValue()
268 {
269 return value;
270 }
271
272 /** Return the value of this <code>Byte</code> as a <code>float</code>.
273 ** @return the value of this <code>Byte</code> as a <code>float</code>.
274 **/
275 public float floatValue()
276 {
277 return value;
278 }
279
280 /** Return the value of this <code>Byte</code> as a <code>double</code>.
281 ** @return the value of this <code>Byte</code> as a <code>double</code>.
282 **/
283 public double doubleValue()
284 {
285 return value;
286 }
287
288 /**
289 * Compare two Bytes numerically by comparing their
290 * <code>byte</code> values.
291 * @return a positive value if this <code>Byte</code> is greater
292 * in value than the argument <code>Byte</code>; a negative value
293 * if this <code>Byte</code> is smaller in value than the argument
294 * <code>Byte</code>; and <code>0</code>, zero, if this
295 * <code>Byte</code> is equal in value to the argument
296 * <code>Byte</code>.
297 */
298 public int compareTo(Byte b)
299 {
300 return (int)(value - b.byteValue());
301 }
302
303 /**
304 * Behaves like <code>compareTo(java.lang.Byte)</code> unless the Object
305 * is not a <code>Byte</code>. Then it throws a
306 * <code>ClassCastException</code>.
307 * @exception ClassCastException if the argument is not a
308 * <code>Byte</code>.
309 */
310 public int compareTo(Object o)
311 {
312 return compareTo((Byte)o);
313 }
314}
Note: See TracBrowser for help on using the repository browser.