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