1. /*
  2. * @(#)Byte.java 1.40 04/05/11
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. *
  10. * The <code>Byte</code> class wraps a value of primitive type
  11. * <code>byte</code> in an object. An object of type
  12. * <code>Byte</code> contains a single field whose type is
  13. * <code>byte</code>.
  14. *
  15. * <p>
  16. *
  17. * In addition, this class provides several methods for converting a
  18. * <code>byte</code> to a <code>String</code> and a <code>String</code>
  19. * to a <code>byte</code>, as well as other constants and methods
  20. * useful when dealing with a <code>byte</code>.
  21. *
  22. * @author Nakul Saraiya
  23. * @version 1.40, 05/11/04
  24. * @see java.lang.Number
  25. * @since JDK1.1
  26. */
  27. public final class Byte extends Number implements Comparable<Byte> {
  28. /**
  29. * A constant holding the minimum value a <code>byte</code> can
  30. * have, -2<sup>7</sup>.
  31. */
  32. public static final byte MIN_VALUE = -128;
  33. /**
  34. * A constant holding the maximum value a <code>byte</code> can
  35. * have, 2<sup>7</sup>-1.
  36. */
  37. public static final byte MAX_VALUE = 127;
  38. /**
  39. * The <code>Class</code> instance representing the primitive type
  40. * <code>byte</code>.
  41. */
  42. public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
  43. /**
  44. * Returns a new <code>String</code> object representing the
  45. * specified <code>byte</code>. The radix is assumed to be 10.
  46. *
  47. * @param b the <code>byte</code> to be converted
  48. * @return the string representation of the specified <code>byte</code>
  49. * @see java.lang.Integer#toString(int)
  50. */
  51. public static String toString(byte b) {
  52. return Integer.toString((int)b, 10);
  53. }
  54. private static class ByteCache {
  55. private ByteCache(){}
  56. static final Byte cache[] = new Byte[-(-128) + 127 + 1];
  57. static {
  58. for(int i = 0; i < cache.length; i++)
  59. cache[i] = new Byte((byte)(i - 128));
  60. }
  61. }
  62. /**
  63. * Returns a <tt>Byte</tt> instance representing the specified
  64. * <tt>byte</tt> value.
  65. * If a new <tt>Byte</tt> instance is not required, this method
  66. * should generally be used in preference to the constructor
  67. * {@link #Byte(byte)}, as this method is likely to yield
  68. * significantly better space and time performance by caching
  69. * frequently requested values.
  70. *
  71. * @param b a byte value.
  72. * @return a <tt>Byte</tt> instance representing <tt>b</tt>.
  73. * @since 1.5
  74. */
  75. public static Byte valueOf(byte b) {
  76. final int offset = 128;
  77. return ByteCache.cache[(int)b + offset];
  78. }
  79. /**
  80. * Parses the string argument as a signed decimal
  81. * <code>byte</code>. The characters in the string must all be
  82. * decimal digits, except that the first character may be an ASCII
  83. * minus sign <code>'-'</code> (<code>'\u002D'</code>) to
  84. * indicate a negative value. The resulting <code>byte</code> value is
  85. * returned, exactly as if the argument and the radix 10 were
  86. * given as arguments to the {@link #parseByte(java.lang.String,
  87. * int)} method.
  88. *
  89. * @param s a <code>String</code> containing the
  90. * <code>byte</code> representation to be parsed
  91. * @return the <code>byte</code> value represented by the
  92. * argument in decimal
  93. * @exception NumberFormatException if the string does not
  94. * contain a parsable <code>byte</code>.
  95. */
  96. public static byte parseByte(String s) throws NumberFormatException {
  97. return parseByte(s, 10);
  98. }
  99. /**
  100. * Parses the string argument as a signed <code>byte</code> in the
  101. * radix specified by the second argument. The characters in the
  102. * string must all be digits, of the specified radix (as
  103. * determined by whether {@link java.lang.Character#digit(char,
  104. * int)} returns a nonnegative value) except that the first
  105. * character may be an ASCII minus sign <code>'-'</code>
  106. * (<code>'\u002D'</code>) to indicate a negative value. The
  107. * resulting <code>byte</code> value is returned.
  108. * <p>
  109. * An exception of type <code>NumberFormatException</code> is
  110. * thrown if any of the following situations occurs:
  111. * <ul>
  112. * <li> The first argument is <code>null</code> or is a string of
  113. * length zero.
  114. *
  115. * <li> The radix is either smaller than {@link
  116. * java.lang.Character#MIN_RADIX} or larger than {@link
  117. * java.lang.Character#MAX_RADIX}.
  118. *
  119. * <li> Any character of the string is not a digit of the specified
  120. * radix, except that the first character may be a minus sign
  121. * <code>'-'</code> (<code>'\u002D'</code>) provided that the
  122. * string is longer than length 1.
  123. *
  124. * <li> The value represented by the string is not a value of type
  125. * <code>byte</code>.
  126. * </ul>
  127. *
  128. * @param s the <code>String</code> containing the
  129. * <code>byte</code>
  130. * representation to be parsed
  131. * @param radix the radix to be used while parsing <code>s</code>
  132. * @return the <code>byte</code> value represented by the string
  133. * argument in the specified radix
  134. * @exception NumberFormatException If the string does
  135. * not contain a parsable <code>byte</code>.
  136. */
  137. public static byte parseByte(String s, int radix)
  138. throws NumberFormatException {
  139. int i = Integer.parseInt(s, radix);
  140. if (i < MIN_VALUE || i > MAX_VALUE)
  141. throw new NumberFormatException(
  142. "Value out of range. Value:\"" + s + "\" Radix:" + radix);
  143. return (byte)i;
  144. }
  145. /**
  146. * Returns a <code>Byte</code> object holding the value
  147. * extracted from the specified <code>String</code> when parsed
  148. * with the radix given by the second argument. The first argument
  149. * is interpreted as representing a signed <code>byte</code> in
  150. * the radix specified by the second argument, exactly as if the
  151. * argument were given to the {@link #parseByte(java.lang.String,
  152. * int)} method. The result is a <code>Byte</code> object that
  153. * represents the <code>byte</code> value specified by the string.
  154. * <p> In other words, this method returns a <code>Byte</code> object
  155. * equal to the value of:
  156. *
  157. * <blockquote><code>
  158. * new Byte(Byte.parseByte(s, radix))
  159. * </code></blockquote>
  160. *
  161. * @param s the string to be parsed
  162. * @param radix the radix to be used in interpreting <code>s</code>
  163. * @return a <code>Byte</code> object holding the value
  164. * represented by the string argument in the
  165. * specified radix.
  166. * @exception NumberFormatException If the <code>String</code> does
  167. * not contain a parsable <code>byte</code>.
  168. */
  169. public static Byte valueOf(String s, int radix)
  170. throws NumberFormatException {
  171. return new Byte(parseByte(s, radix));
  172. }
  173. /**
  174. * Returns a <code>Byte</code> object holding the value
  175. * given by the specified <code>String</code>. The argument is
  176. * interpreted as representing a signed decimal <code>byte</code>,
  177. * exactly as if the argument were given to the {@link
  178. * #parseByte(java.lang.String)} method. The result is a
  179. * <code>Byte</code> object that represents the <code>byte</code>
  180. * value specified by the string. <p> In other words, this method
  181. * returns a <code>Byte</code> object equal to the value of:
  182. *
  183. * <blockquote><code>
  184. * new Byte(Byte.parseByte(s))
  185. * </code></blockquote>
  186. *
  187. * @param s the string to be parsed
  188. * @return a <code>Byte</code> object holding the value
  189. * represented by the string argument
  190. * @exception NumberFormatException If the <code>String</code> does
  191. * not contain a parsable <code>byte</code>.
  192. */
  193. public static Byte valueOf(String s) throws NumberFormatException {
  194. return valueOf(s, 10);
  195. }
  196. /**
  197. * Decodes a <code>String</code> into a <code>Byte</code>.
  198. * Accepts decimal, hexadecimal, and octal numbers given by
  199. * the following grammar:
  200. *
  201. * <blockquote>
  202. * <dl>
  203. * <dt><i>DecodableString:</i>
  204. * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
  205. * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
  206. * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
  207. * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
  208. * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
  209. * <p>
  210. * <dt><i>Sign:</i>
  211. * <dd><code>-</code>
  212. * </dl>
  213. * </blockquote>
  214. *
  215. * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
  216. * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a>
  217. * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
  218. * Language Specification</a>.
  219. * <p>
  220. * The sequence of characters following an (optional) negative
  221. * sign and/or radix specifier ("<code>0x</code>",
  222. * "<code>0X</code>", "<code>#</code>", or
  223. * leading zero) is parsed as by the <code>Byte.parseByte</code>
  224. * method with the indicated radix (10, 16, or 8). This sequence
  225. * of characters must represent a positive value or a {@link
  226. * NumberFormatException} will be thrown. The result is negated
  227. * if first character of the specified <code>String</code> is the
  228. * minus sign. No whitespace characters are permitted in the
  229. * <code>String</code>.
  230. *
  231. * @param nm the <code>String</code> to decode.
  232. * @return a <code>Byte</code> object holding the <code>byte</code>
  233. * value represented by <code>nm</code>
  234. * @exception NumberFormatException if the <code>String</code> does not
  235. * contain a parsable <code>byte</code>.
  236. * @see java.lang.Byte#parseByte(java.lang.String, int)
  237. */
  238. public static Byte decode(String nm) throws NumberFormatException {
  239. int radix = 10;
  240. int index = 0;
  241. boolean negative = false;
  242. Byte result;
  243. // Handle minus sign, if present
  244. if (nm.startsWith("-")) {
  245. negative = true;
  246. index++;
  247. }
  248. if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  249. index += 2;
  250. radix = 16;
  251. } else if (nm.startsWith("#", index)) {
  252. index++;
  253. radix = 16;
  254. } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  255. index++;
  256. radix = 8;
  257. }
  258. if (nm.startsWith("-", index))
  259. throw new NumberFormatException("Negative sign in wrong position");
  260. try {
  261. result = Byte.valueOf(nm.substring(index), radix);
  262. result = negative ? new Byte((byte)-result.byteValue()) : result;
  263. } catch (NumberFormatException e) {
  264. // If number is Byte.MIN_VALUE, we'll end up here. The next line
  265. // handles this case, and causes any genuine format error to be
  266. // rethrown.
  267. String constant = negative ? new String("-" + nm.substring(index))
  268. : nm.substring(index);
  269. result = Byte.valueOf(constant, radix);
  270. }
  271. return result;
  272. }
  273. /**
  274. * The value of the <code>Byte</code>.
  275. *
  276. * @serial
  277. */
  278. private final byte value;
  279. /**
  280. * Constructs a newly allocated <code>Byte</code> object that
  281. * represents the specified <code>byte</code> value.
  282. *
  283. * @param value the value to be represented by the
  284. * <code>Byte</code>.
  285. */
  286. public Byte(byte value) {
  287. this.value = value;
  288. }
  289. /**
  290. * Constructs a newly allocated <code>Byte</code> object that
  291. * represents the <code>byte</code> value indicated by the
  292. * <code>String</code> parameter. The string is converted to a
  293. * <code>byte</code> value in exactly the manner used by the
  294. * <code>parseByte</code> method for radix 10.
  295. *
  296. * @param s the <code>String</code> to be converted to a
  297. * <code>Byte</code>
  298. * @exception NumberFormatException If the <code>String</code>
  299. * does not contain a parsable <code>byte</code>.
  300. * @see java.lang.Byte#parseByte(java.lang.String, int)
  301. */
  302. public Byte(String s) throws NumberFormatException {
  303. this.value = parseByte(s, 10);
  304. }
  305. /**
  306. * Returns the value of this <code>Byte</code> as a
  307. * <code>byte</code>.
  308. */
  309. public byte byteValue() {
  310. return value;
  311. }
  312. /**
  313. * Returns the value of this <code>Byte</code> as a
  314. * <code>short</code>.
  315. */
  316. public short shortValue() {
  317. return (short)value;
  318. }
  319. /**
  320. * Returns the value of this <code>Byte</code> as an
  321. * <code>int</code>.
  322. */
  323. public int intValue() {
  324. return (int)value;
  325. }
  326. /**
  327. * Returns the value of this <code>Byte</code> as a
  328. * <code>long</code>.
  329. */
  330. public long longValue() {
  331. return (long)value;
  332. }
  333. /**
  334. * Returns the value of this <code>Byte</code> as a
  335. * <code>float</code>.
  336. */
  337. public float floatValue() {
  338. return (float)value;
  339. }
  340. /**
  341. * Returns the value of this <code>Byte</code> as a
  342. * <code>double</code>.
  343. */
  344. public double doubleValue() {
  345. return (double)value;
  346. }
  347. /**
  348. * Returns a <code>String</code> object representing this
  349. * <code>Byte</code>'s value. The value is converted to signed
  350. * decimal representation and returned as a string, exactly as if
  351. * the <code>byte</code> value were given as an argument to the
  352. * {@link java.lang.Byte#toString(byte)} method.
  353. *
  354. * @return a string representation of the value of this object in
  355. * base 10.
  356. */
  357. public String toString() {
  358. return String.valueOf((int)value);
  359. }
  360. /**
  361. * Returns a hash code for this <code>Byte</code>.
  362. */
  363. public int hashCode() {
  364. return (int)value;
  365. }
  366. /**
  367. * Compares this object to the specified object. The result is
  368. * <code>true</code> if and only if the argument is not
  369. * <code>null</code> and is a <code>Byte</code> object that
  370. * contains the same <code>byte</code> value as this object.
  371. *
  372. * @param obj the object to compare with
  373. * @return <code>true</code> if the objects are the same;
  374. * <code>false</code> otherwise.
  375. */
  376. public boolean equals(Object obj) {
  377. if (obj instanceof Byte) {
  378. return value == ((Byte)obj).byteValue();
  379. }
  380. return false;
  381. }
  382. /**
  383. * Compares two <code>Byte</code> objects numerically.
  384. *
  385. * @param anotherByte the <code>Byte</code> to be compared.
  386. * @return the value <code>0</code> if this <code>Byte</code> is
  387. * equal to the argument <code>Byte</code> a value less than
  388. * <code>0</code> if this <code>Byte</code> is numerically less
  389. * than the argument <code>Byte</code> and a value greater than
  390. * <code>0</code> if this <code>Byte</code> is numerically
  391. * greater than the argument <code>Byte</code> (signed
  392. * comparison).
  393. * @since 1.2
  394. */
  395. public int compareTo(Byte anotherByte) {
  396. return this.value - anotherByte.value;
  397. }
  398. /**
  399. * The number of bits used to represent a <tt>byte</tt> value in two's
  400. * complement binary form.
  401. *
  402. * @since 1.5
  403. */
  404. public static final int SIZE = 8;
  405. /** use serialVersionUID from JDK 1.1. for interoperability */
  406. private static final long serialVersionUID = -7183698231559129828L;
  407. }