OSDN Git Service

Imported GNU Classpath 0.19 + gcj-import-20051115.
[pf3gnuchains/gcc-fork.git] / libjava / classpath / java / lang / Byte.java
index 338e216..2560bfc 100644 (file)
@@ -50,7 +50,7 @@ package java.lang;
  * @author Per Bothner
  * @author Eric Blake (ebb9@email.byu.edu)
  * @since 1.1
- * @status updated to 1.4
+ * @status updated to 1.5
  */
 public final class Byte extends Number implements Comparable
 {
@@ -78,6 +78,16 @@ public final class Byte extends Number implements Comparable
   public static final Class TYPE = VMClassLoader.getPrimitiveClass('B');
 
   /**
+   * The number of bits needed to represent a <code>byte</code>.
+   * @since 1.5
+   */
+  public static final int SIZE = 8;
+
+  // This caches Byte values, and is used by boxing conversions via
+  // valueOf().  We're required to cache all possible values here.
+  private static Byte[] byteCache = new Byte[MAX_VALUE - MIN_VALUE + 1];
+
+  /**
    * The immutable value of this Byte.
    *
    * @serial the wrapped byte
@@ -192,6 +202,26 @@ public final class Byte extends Number implements Comparable
   }
 
   /**
+   * Returns a <code>Byte</code> object wrapping the value.
+   * In contrast to the <code>Byte</code> constructor, this method
+   * will cache some values.  It is used by boxing conversion.
+   *
+   * @param val the value to wrap
+   * @return the <code>Byte</code>
+   * 
+   * @since 1.5
+   */
+  public static Byte valueOf(byte val)
+  {
+    synchronized (byteCache)
+      {
+    if (byteCache[val - MIN_VALUE] == null)
+      byteCache[val - MIN_VALUE] = new Byte(val);
+    return byteCache[val - MIN_VALUE];
+      }
+  }
+
+ /**
    * Convert the specified <code>String</code> into a <code>Byte</code>.
    * The <code>String</code> may represent decimal, hexadecimal, or
    * octal numbers.