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.

+ * @since 1.0 */ public final class Byte extends Number implements Comparable { - + //TODO Add Comparable to implements when generics are supported. private static final long serialVersionUID = -7183698231559129828L; /** @@ -26,14 +26,31 @@ /** * The value which the receiver represents. */ - final byte value; + private final byte value; /** - * Most positive and most negative possible byte values. - */ - public static final byte MAX_VALUE = (byte) 0x7F; + *

+ * Constant for the maximum byte value, 27-1. + *

+ */ + public static final byte MAX_VALUE = (byte) 0x7F; - public static final byte MIN_VALUE = (byte) 0x80; + /** + *

+ * Constant for the minimum byte value, -27. + *

+ */ + public static final byte MIN_VALUE = (byte) 0x80; + + /** + *

+ * Constant for the number of bits to represent a byte in + * two's compliment form. + *

+ * + * @since 1.5 + */ + public static final int SIZE = 8; /** * The java.lang.Class that represents this class. @@ -42,6 +59,13 @@ // Note: This can't be set to "byte.class", since *that* is // defined to be "java.lang.Byte.TYPE"; + + /** + *

+ * 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.

+ * @param b The byte value. + * @return A 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); + } + } }