src/share/classes/java/lang/Byte.java

Print this page




  73         return Integer.toString((int)b, 10);
  74     }
  75 
  76     private static class ByteCache {
  77         private ByteCache(){}
  78 
  79         static final Byte cache[] = new Byte[-(-128) + 127 + 1];
  80 
  81         static {
  82             for(int i = 0; i < cache.length; i++)
  83                 cache[i] = new Byte((byte)(i - 128));
  84         }
  85     }
  86 
  87     /**
  88      * Returns a {@code Byte} instance representing the specified
  89      * {@code byte} value.
  90      * If a new {@code Byte} instance is not required, this method
  91      * should generally be used in preference to the constructor
  92      * {@link #Byte(byte)}, as this method is likely to yield
  93      * significantly better space and time performance by caching
  94      * frequently requested values.
  95      *
  96      * @param  b a byte value.
  97      * @return a {@code Byte} instance representing {@code b}.
  98      * @since  1.5
  99      */
 100     public static Byte valueOf(byte b) {
 101         final int offset = 128;
 102         return ByteCache.cache[(int)b + offset];
 103     }
 104 
 105     /**
 106      * Parses the string argument as a signed {@code byte} in the
 107      * radix specified by the second argument. The characters in the
 108      * string must all be digits, of the specified radix (as
 109      * determined by whether {@link java.lang.Character#digit(char,
 110      * int)} returns a nonnegative value) except that the first
 111      * character may be an ASCII minus sign {@code '-'}
 112      * (<code>'&#92;u002D'</code>) to indicate a negative value or an
 113      * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
 114      * indicate a positive value.  The resulting {@code byte} value is




  73         return Integer.toString((int)b, 10);
  74     }
  75 
  76     private static class ByteCache {
  77         private ByteCache(){}
  78 
  79         static final Byte cache[] = new Byte[-(-128) + 127 + 1];
  80 
  81         static {
  82             for(int i = 0; i < cache.length; i++)
  83                 cache[i] = new Byte((byte)(i - 128));
  84         }
  85     }
  86 
  87     /**
  88      * Returns a {@code Byte} instance representing the specified
  89      * {@code byte} value.
  90      * If a new {@code Byte} instance is not required, this method
  91      * should generally be used in preference to the constructor
  92      * {@link #Byte(byte)}, as this method is likely to yield
  93      * significantly better space and time performance since
  94      * all byte values are cached.
  95      *
  96      * @param  b a byte value.
  97      * @return a {@code Byte} instance representing {@code b}.
  98      * @since  1.5
  99      */
 100     public static Byte valueOf(byte b) {
 101         final int offset = 128;
 102         return ByteCache.cache[(int)b + offset];
 103     }
 104 
 105     /**
 106      * Parses the string argument as a signed {@code byte} in the
 107      * radix specified by the second argument. The characters in the
 108      * string must all be digits, of the specified radix (as
 109      * determined by whether {@link java.lang.Character#digit(char,
 110      * int)} returns a nonnegative value) except that the first
 111      * character may be an ASCII minus sign {@code '-'}
 112      * (<code>'&#92;u002D'</code>) to indicate a negative value or an
 113      * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
 114      * indicate a positive value.  The resulting {@code byte} value is