< prev index next >

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

Print this page
rev 13019 : 8141678: sun.invoke.util.Wrapper eagerly initializes all integral type caches
Reviewed-by: TBD


  48     /**
  49      * A constant holding the minimum value a {@code byte} can
  50      * have, -2<sup>7</sup>.
  51      */
  52     public static final byte   MIN_VALUE = -128;
  53 
  54     /**
  55      * A constant holding the maximum value a {@code byte} can
  56      * have, 2<sup>7</sup>-1.
  57      */
  58     public static final byte   MAX_VALUE = 127;
  59 
  60     /**
  61      * The {@code Class} instance representing the primitive type
  62      * {@code byte}.
  63      */
  64     @SuppressWarnings("unchecked")
  65     public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
  66 
  67     /**





  68      * Returns a new {@code String} object representing the
  69      * specified {@code byte}. The radix is assumed to be 10.
  70      *
  71      * @param b the {@code byte} to be converted
  72      * @return the string representation of the specified {@code byte}
  73      * @see java.lang.Integer#toString(int)
  74      */
  75     public static String toString(byte b) {
  76         return Integer.toString((int)b, 10);
  77     }
  78 
  79     private static class ByteCache {
  80         private ByteCache(){}
  81 
  82         static final Byte cache[] = new Byte[-(-128) + 127 + 1];
  83 
  84         static {
  85             for(int i = 0; i < cache.length; i++)
  86                 cache[i] = new Byte((byte)(i - 128));


  87         }
  88     }
  89 
  90     /**
  91      * Returns a {@code Byte} instance representing the specified
  92      * {@code byte} value.
  93      * If a new {@code Byte} instance is not required, this method
  94      * should generally be used in preference to the constructor
  95      * {@link #Byte(byte)}, as this method is likely to yield
  96      * significantly better space and time performance since
  97      * all byte values are cached.
  98      *
  99      * @param  b a byte value.
 100      * @return a {@code Byte} instance representing {@code b}.
 101      * @since  1.5
 102      */
 103     @HotSpotIntrinsicCandidate
 104     public static Byte valueOf(byte b) {
 105         final int offset = 128;
 106         return ByteCache.cache[(int)b + offset];




  48     /**
  49      * A constant holding the minimum value a {@code byte} can
  50      * have, -2<sup>7</sup>.
  51      */
  52     public static final byte   MIN_VALUE = -128;
  53 
  54     /**
  55      * A constant holding the maximum value a {@code byte} can
  56      * have, 2<sup>7</sup>-1.
  57      */
  58     public static final byte   MAX_VALUE = 127;
  59 
  60     /**
  61      * The {@code Class} instance representing the primitive type
  62      * {@code byte}.
  63      */
  64     @SuppressWarnings("unchecked")
  65     public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
  66 
  67     /**
  68      * Zero {@code Byte} constant.
  69      */
  70     private static final Byte ZERO = new Byte((byte)0);
  71 
  72     /**
  73      * Returns a new {@code String} object representing the
  74      * specified {@code byte}. The radix is assumed to be 10.
  75      *
  76      * @param b the {@code byte} to be converted
  77      * @return the string representation of the specified {@code byte}
  78      * @see java.lang.Integer#toString(int)
  79      */
  80     public static String toString(byte b) {
  81         return Integer.toString((int)b, 10);
  82     }
  83 
  84     private static class ByteCache {
  85         private ByteCache(){}
  86 
  87         static final Byte cache[] = new Byte[-(-128) + 127 + 1];
  88 
  89         static {
  90             for(int i = 0; i < cache.length; i++) {
  91                 int val = i - 128;
  92                 cache[i] = (val == 0) ? ZERO : new Byte((byte)val);
  93             }
  94         }
  95     }
  96 
  97     /**
  98      * Returns a {@code Byte} instance representing the specified
  99      * {@code byte} value.
 100      * If a new {@code Byte} instance is not required, this method
 101      * should generally be used in preference to the constructor
 102      * {@link #Byte(byte)}, as this method is likely to yield
 103      * significantly better space and time performance since
 104      * all byte values are cached.
 105      *
 106      * @param  b a byte value.
 107      * @return a {@code Byte} instance representing {@code b}.
 108      * @since  1.5
 109      */
 110     @HotSpotIntrinsicCandidate
 111     public static Byte valueOf(byte b) {
 112         final int offset = 128;
 113         return ByteCache.cache[(int)b + offset];


< prev index next >