1. /*
  2. * @(#)Byte.java 1.14 01/11/29
  3. *
  4. * Copyright 2002 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 Byte class is the standard wrapper for byte values.
  11. *
  12. * @author Nakul Saraiya
  13. * @version 1.14, 11/29/01
  14. * @see java.lang.Number
  15. * @since JDK1.1
  16. */
  17. public final class Byte extends Number implements Comparable {
  18. /**
  19. * The minimum value a Byte can have.
  20. */
  21. public static final byte MIN_VALUE = -128;
  22. /**
  23. * The maximum value a Byte can have.
  24. */
  25. public static final byte MAX_VALUE = 127;
  26. /**
  27. * The Class object representing the primitive type byte.
  28. */
  29. public static final Class TYPE = Class.getPrimitiveClass("byte");
  30. /**
  31. * Returns a new String object representing the specified Byte. The radix
  32. * is assumed to be 10.
  33. *
  34. * @param b the byte to be converted
  35. */
  36. public static String toString(byte b) {
  37. return Integer.toString((int)b, 10);
  38. }
  39. /**
  40. * Assuming the specified String represents a byte, returns
  41. * that byte's value. Throws an exception if the String cannot
  42. * be parsed as a byte. The radix is assumed to be 10.
  43. *
  44. * @param s the String containing the byte
  45. * @exception NumberFormatException If the string does not
  46. * contain a parsable byte.
  47. */
  48. public static byte parseByte(String s) throws NumberFormatException {
  49. return parseByte(s, 10);
  50. }
  51. /**
  52. * Assuming the specified String represents a byte, returns
  53. * that byte's value. Throws an exception if the String cannot
  54. * be parsed as a byte.
  55. *
  56. * @param s the String containing the byte
  57. * @param radix the radix to be used
  58. * @exception NumberFormatException If the String does not
  59. * contain a parsable byte.
  60. */
  61. public static byte parseByte(String s, int radix)
  62. throws NumberFormatException {
  63. int i = Integer.parseInt(s, radix);
  64. if (i < MIN_VALUE || i > MAX_VALUE)
  65. throw new NumberFormatException();
  66. return (byte)i;
  67. }
  68. /**
  69. * Assuming the specified String represents a byte, returns a
  70. * new Byte object initialized to that value. Throws an
  71. * exception if the String cannot be parsed as a byte.
  72. *
  73. * @param s the String containing the integer
  74. * @param radix the radix to be used
  75. * @exception NumberFormatException If the String does not
  76. * contain a parsable byte.
  77. */
  78. public static Byte valueOf(String s, int radix)
  79. throws NumberFormatException {
  80. return new Byte(parseByte(s, radix));
  81. }
  82. /**
  83. * Assuming the specified String represents a byte, returns a
  84. * new Byte object initialized to that value. Throws an
  85. * exception if the String cannot be parsed as a byte.
  86. * The radix is assumed to be 10.
  87. *
  88. * @param s the String containing the integer
  89. * @exception NumberFormatException If the String does not
  90. * contain a parsable byte.
  91. */
  92. public static Byte valueOf(String s) throws NumberFormatException {
  93. return valueOf(s, 10);
  94. }
  95. /**
  96. * Decodes a String into a Byte. The String may represent
  97. * decimal, hexadecimal, and octal numbers.
  98. *
  99. * @param nm the string to decode
  100. */
  101. public static Byte decode(String nm) throws NumberFormatException {
  102. if (nm.startsWith("0x")) {
  103. return Byte.valueOf(nm.substring(2), 16);
  104. }
  105. if (nm.startsWith("#")) {
  106. return Byte.valueOf(nm.substring(1), 16);
  107. }
  108. if (nm.startsWith("0") && nm.length() > 1) {
  109. return Byte.valueOf(nm.substring(1), 8);
  110. }
  111. return Byte.valueOf(nm);
  112. }
  113. /**
  114. * The value of the Byte.
  115. *
  116. * @serial
  117. */
  118. private byte value;
  119. /**
  120. * Constructs a Byte object initialized to the specified byte value.
  121. *
  122. * @param value the initial value of the Byte
  123. */
  124. public Byte(byte value) {
  125. this.value = value;
  126. }
  127. /**
  128. * Constructs a Byte object initialized to the value specified by the
  129. * String parameter. The radix is assumed to be 10.
  130. *
  131. * @param s the String to be converted to a Byte
  132. * @exception NumberFormatException If the String does not
  133. * contain a parsable byte.
  134. */
  135. public Byte(String s) throws NumberFormatException {
  136. this.value = parseByte(s);
  137. }
  138. /**
  139. * Returns the value of this Byte as a byte.
  140. */
  141. public byte byteValue() {
  142. return value;
  143. }
  144. /**
  145. * Returns the value of this Byte as a short.
  146. */
  147. public short shortValue() {
  148. return (short)value;
  149. }
  150. /**
  151. * Returns the value of this Byte as an int.
  152. */
  153. public int intValue() {
  154. return (int)value;
  155. }
  156. /**
  157. * Returns the value of this Byte as a long.
  158. */
  159. public long longValue() {
  160. return (long)value;
  161. }
  162. /**
  163. * Returns the value of this Byte as a float.
  164. */
  165. public float floatValue() {
  166. return (float)value;
  167. }
  168. /**
  169. * Returns the value of this Byte as a double.
  170. */
  171. public double doubleValue() {
  172. return (double)value;
  173. }
  174. /**
  175. * Returns a String object representing this Byte's value.
  176. */
  177. public String toString() {
  178. return String.valueOf((int)value);
  179. }
  180. /**
  181. * Returns a hashcode for this Byte.
  182. */
  183. public int hashCode() {
  184. return (int)value;
  185. }
  186. /**
  187. * Compares this object to the specified object.
  188. *
  189. * @param obj the object to compare with
  190. * @return true if the objects are the same; false otherwise.
  191. */
  192. public boolean equals(Object obj) {
  193. if ((obj != null) && (obj instanceof Byte)) {
  194. return value == ((Byte)obj).byteValue();
  195. }
  196. return false;
  197. }
  198. /**
  199. * Compares two Bytes numerically.
  200. *
  201. * @param anotherByte the <code>Byte</code> to be compared.
  202. * @return the value <code>0</code> if the argument Byte is equal to
  203. * this Byte; a value less than <code>0</code> if this Byte
  204. * is numerically less than the Byte argument; and a
  205. * value greater than <code>0</code> if this Byte is
  206. * numerically greater than the Byte argument (signed comparison).
  207. * @since JDK1.2
  208. */
  209. public int compareTo(Byte anotherByte) {
  210. return this.value - anotherByte.value;
  211. }
  212. /**
  213. * Compares this Byte to another Object. If the Object is a Byte,
  214. * this function behaves like <code>compareTo(Byte)</code>. Otherwise,
  215. * it throws a <code>ClassCastException</code> (as Bytes are comparable
  216. * only to other Bytes).
  217. *
  218. * @param o the <code>Object</code> to be compared.
  219. * @return the value <code>0</code> if the argument is a Byte
  220. * numerically equal to this Byte; a value less than
  221. * <code>0</code> if the argument is a Byte numerically
  222. * greater than this Byte; and a value greater than
  223. * <code>0</code> if the argument is a Byte numerically
  224. * less than this Byte.
  225. * @exception <code>ClassCastException</code> if the argument is not a
  226. * <code>Byte</code>.
  227. * @see java.lang.Comparable
  228. * @since JDK1.2
  229. */
  230. public int compareTo(Object o) {
  231. return compareTo((Byte)o);
  232. }
  233. /** use serialVersionUID from JDK 1.1. for interoperability */
  234. private static final long serialVersionUID = -7183698231559129828L;
  235. }