Index: /luni/src/main/java/java/lang/Byte.java =================================================================== --- /luni/src/main/java/java/lang/Byte.java (revision 383745) +++ /luni/src/main/java/java/lang/Byte.java (working copy) @@ -15,12 +15,12 @@ package java.lang; - /** - * Bytes are objects (i.e. non-base types) which represent byte values. + *
Byte is the wrapper for the primitive type byte.
+ * Constant for the maximum byte value, 27-1.
+ *
+ * Constant for the minimum byte value, -27.
+ *
+ * Constant for the number of bits to represent a byte in
+ * two's compliment form.
+ *
+ * A cache of instances used by {@link #valueOf(byte)} and auto-boxing. + *
+ */ + private static final Byte[] CACHE = new Byte[256]; /** * Constructs a new instance of the receiver which represents the byte @@ -99,7 +123,7 @@ int intValue = Integer.decode(string).intValue(); byte result = (byte) intValue; if (result == intValue) - return new Byte(result); + return valueOf(result); throw new NumberFormatException(); } @@ -255,7 +279,7 @@ * if the argument could not be parsed as a byte quantity. */ public static Byte valueOf(String string) throws NumberFormatException { - return new Byte(parseByte(string)); + return valueOf(parseByte(string)); } /** @@ -274,6 +298,22 @@ */ public static Byte valueOf(String string, int radix) throws NumberFormatException { - return new Byte(parseByte(string, radix)); + return valueOf(parseByte(string, radix)); } + + /** + *Returns a Byte instance for the byte value passed.
+ * This method is preferred over the constructor, as this method may maintain a cache
+ * of instances.
Byte instance.
+ * @since 1.5
+ */
+ public static Byte valueOf(byte b) {
+ synchronized (CACHE) {
+ int idx = b - MIN_VALUE;
+ Byte result = CACHE[idx];
+ return (result == null ? CACHE[idx] = new Byte(b) : result);
+ }
+ }
}