1. /*
  2. * @(#)Byte.java 1.20 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. /**
  12. *
  13. * The Byte class is the standard wrapper for byte values.
  14. *
  15. * @author Nakul Saraiya
  16. * @version 1.20, 02/02/00
  17. * @see java.lang.Number
  18. * @since JDK1.1
  19. */
  20. public final class Byte extends Number implements Comparable {
  21. /**
  22. * The minimum value a Byte can have.
  23. */
  24. public static final byte MIN_VALUE = -128;
  25. /**
  26. * The maximum value a Byte can have.
  27. */
  28. public static final byte MAX_VALUE = 127;
  29. /**
  30. * The Class object representing the primitive type byte.
  31. */
  32. public static final Class TYPE = Class.getPrimitiveClass("byte");
  33. /**
  34. * Returns a new String object representing the specified Byte. The radix
  35. * is assumed to be 10.
  36. *
  37. * @param b the byte to be converted
  38. * @return the string representation of the specified <code>byte</code>
  39. */
  40. public static String toString(byte b) {
  41. return Integer.toString((int)b, 10);
  42. }
  43. /**
  44. * Assuming the specified String represents a byte, returns
  45. * that byte's value. Throws an exception if the String cannot
  46. * be parsed as a byte. The radix is assumed to be 10.
  47. *
  48. * @param s the String containing the byte
  49. * @return the parsed value of the byte
  50. * @exception NumberFormatException If the string does not
  51. * contain a parsable byte.
  52. */
  53. public static byte parseByte(String s) throws NumberFormatException {
  54. return parseByte(s, 10);
  55. }
  56. /**
  57. * Assuming the specified String represents a byte, returns
  58. * that byte's value. Throws an exception if the String cannot
  59. * be parsed as a byte.
  60. *
  61. * @param s the String containing the byte
  62. * @param radix the radix to be used
  63. * @return the parsed value of the byte
  64. * @exception NumberFormatException If the String does not
  65. * contain a parsable byte.
  66. */
  67. public static byte parseByte(String s, int radix)
  68. throws NumberFormatException {
  69. int i = Integer.parseInt(s, radix);
  70. if (i < MIN_VALUE || i > MAX_VALUE)
  71. throw new NumberFormatException();
  72. return (byte)i;
  73. }
  74. /**
  75. * Assuming the specified String represents a byte, returns a
  76. * new Byte object initialized to that value. Throws an
  77. * exception if the String cannot be parsed as a byte.
  78. *
  79. * @param s the String containing the integer
  80. * @param radix the radix to be used
  81. * @return the <code>Byte</code> instance representing the parsed
  82. * <code>byte</code> value
  83. * @exception NumberFormatException If the String does not
  84. * contain a parsable byte.
  85. */
  86. public static Byte valueOf(String s, int radix)
  87. throws NumberFormatException {
  88. return new Byte(parseByte(s, radix));
  89. }
  90. /**
  91. * Assuming the specified String represents a byte, returns a
  92. * new Byte object initialized to that value. Throws an
  93. * exception if the String cannot be parsed as a byte.
  94. * The radix is assumed to be 10.
  95. *
  96. * @param s the String containing the integer
  97. * @return the <code>Byte</code> instance representing the parsed
  98. * <code>byte</code> value
  99. * @exception NumberFormatException If the String does not
  100. * contain a parsable byte.
  101. */
  102. public static Byte valueOf(String s) throws NumberFormatException {
  103. return valueOf(s, 10);
  104. }
  105. /**
  106. * Decodes a <code>String</code> into a <code>Byte</code>. Accepts
  107. * decimal, hexadecimal, and octal numbers, in the following formats:
  108. * <pre>
  109. * [-] decimal constant
  110. * [-] 0x hex constant
  111. * [-] # hex constant
  112. * [-] 0 octal constant
  113. * </pre>
  114. *
  115. * The constant following an (optional) negative sign and/or "radix
  116. * specifier" is parsed as by the <code>Byte.parseByte</code> method
  117. * with the specified radix (10, 8 or 16). This constant must be positive
  118. * or a NumberFormatException will result. The result is made negative if
  119. * first character of the specified <code>String</code> is the negative
  120. * sign. No whitespace characters are permitted in the
  121. * <code>String</code>.
  122. *
  123. * @param nm the <code>String</code> to decode.
  124. * @return the <code>Byte</code> represented by the specified string.
  125. * @exception NumberFormatException if the <code>String</code> does not
  126. * contain a parsable byte.
  127. * @see java.lang.Byte#parseByte(String, int)
  128. */
  129. public static Byte decode(String nm) throws NumberFormatException {
  130. int radix = 10;
  131. int index = 0;
  132. boolean negative = false;
  133. Byte result;
  134. // Handle minus sign, if present
  135. if (nm.startsWith("-")) {
  136. negative = true;
  137. index++;
  138. }
  139. if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  140. index += 2;
  141. radix = 16;
  142. } else if (nm.startsWith("#", index)) {
  143. index++;
  144. radix = 16;
  145. } else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  146. index++;
  147. radix = 8;
  148. }
  149. if (nm.startsWith("-", index))
  150. throw new NumberFormatException("Negative sign in wrong position");
  151. try {
  152. result = Byte.valueOf(nm.substring(index), radix);
  153. result = negative ? new Byte((byte)-result.byteValue()) : result;
  154. } catch (NumberFormatException e) {
  155. // If number is Byte.MIN_VALUE, we'll end up here. The next line
  156. // handles this case, and causes any genuine format error to be
  157. // rethrown.
  158. String constant = negative ? new String("-" + nm.substring(index))
  159. : nm.substring(index);
  160. result = Byte.valueOf(constant, radix);
  161. }
  162. return result;
  163. }
  164. /**
  165. * The value of the Byte.
  166. *
  167. * @serial
  168. */
  169. private byte value;
  170. /**
  171. * Constructs a Byte object initialized to the specified byte value.
  172. *
  173. * @param value the initial value of the Byte
  174. */
  175. public Byte(byte value) {
  176. this.value = value;
  177. }
  178. /**
  179. * Constructs a Byte object initialized to the value specified by the
  180. * String parameter. The radix is assumed to be 10.
  181. *
  182. * @param s the String to be converted to a Byte
  183. * @exception NumberFormatException If the String does not
  184. * contain a parsable byte.
  185. */
  186. public Byte(String s) throws NumberFormatException {
  187. this.value = parseByte(s);
  188. }
  189. /**
  190. * Returns the value of this Byte as a byte.
  191. */
  192. public byte byteValue() {
  193. return value;
  194. }
  195. /**
  196. * Returns the value of this Byte as a short.
  197. */
  198. public short shortValue() {
  199. return (short)value;
  200. }
  201. /**
  202. * Returns the value of this Byte as an int.
  203. */
  204. public int intValue() {
  205. return (int)value;
  206. }
  207. /**
  208. * Returns the value of this Byte as a long.
  209. */
  210. public long longValue() {
  211. return (long)value;
  212. }
  213. /**
  214. * Returns the value of this Byte as a float.
  215. */
  216. public float floatValue() {
  217. return (float)value;
  218. }
  219. /**
  220. * Returns the value of this Byte as a double.
  221. */
  222. public double doubleValue() {
  223. return (double)value;
  224. }
  225. /**
  226. * Returns a String object representing this Byte's value.
  227. */
  228. public String toString() {
  229. return String.valueOf((int)value);
  230. }
  231. /**
  232. * Returns a hashcode for this Byte.
  233. */
  234. public int hashCode() {
  235. return (int)value;
  236. }
  237. /**
  238. * Compares this object to the specified object.
  239. *
  240. * @param obj the object to compare with
  241. * @return true if the objects are the same; false otherwise.
  242. */
  243. public boolean equals(Object obj) {
  244. if (obj instanceof Byte) {
  245. return value == ((Byte)obj).byteValue();
  246. }
  247. return false;
  248. }
  249. /**
  250. * Compares two Bytes numerically.
  251. *
  252. * @param anotherByte the <code>Byte</code> to be compared.
  253. * @return the value <code>0</code> if the argument Byte is equal to
  254. * this Byte; a value less than <code>0</code> if this Byte
  255. * is numerically less than the Byte argument; and a
  256. * value greater than <code>0</code> if this Byte is
  257. * numerically greater than the Byte argument (signed comparison).
  258. * @since 1.2
  259. */
  260. public int compareTo(Byte anotherByte) {
  261. return this.value - anotherByte.value;
  262. }
  263. /**
  264. * Compares this Byte to another Object. If the Object is a Byte,
  265. * this function behaves like <code>compareTo(Byte)</code>. Otherwise,
  266. * it throws a <code>ClassCastException</code> (as Bytes are comparable
  267. * only to other Bytes).
  268. *
  269. * @param o the <code>Object</code> to be compared.
  270. * @return the value <code>0</code> if the argument is a Byte
  271. * numerically equal to this Byte; a value less than
  272. * <code>0</code> if the argument is a Byte numerically
  273. * greater than this Byte; and a value greater than
  274. * <code>0</code> if the argument is a Byte numerically
  275. * less than this Byte.
  276. * @exception <code>ClassCastException</code> if the argument is not a
  277. * <code>Byte</code>.
  278. * @see java.lang.Comparable
  279. * @since 1.2
  280. */
  281. public int compareTo(Object o) {
  282. return compareTo((Byte)o);
  283. }
  284. /** use serialVersionUID from JDK 1.1. for interoperability */
  285. private static final long serialVersionUID = -7183698231559129828L;
  286. }