< prev index next >

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

Print this page

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang;
 27 
 28 import jdk.internal.misc.CDS;


 29 import jdk.internal.vm.annotation.IntrinsicCandidate;
 30 import jdk.internal.vm.annotation.Stable;
 31 
 32 import java.lang.constant.Constable;
 33 import java.lang.constant.DynamicConstantDesc;
 34 import java.util.Optional;
 35 
 36 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
 37 import static java.lang.constant.ConstantDescs.CD_byte;
 38 import static java.lang.constant.ConstantDescs.CD_int;
 39 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
 40 
 41 /**
 42  * The {@code Byte} class is the {@linkplain
 43  * java.lang##wrapperClass wrapper class} for values of the primitive
 44  * type {@code byte}. An object of type {@code Byte} contains a
 45  * single field whose type is {@code byte}.
 46  *
 47  * <p>In addition, this class provides several methods for converting
 48  * a {@code byte} to a {@code String} and a {@code String} to a {@code
 49  * byte}, as well as other constants and methods useful when dealing
 50  * with a {@code byte}.
 51  *
 52  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 53  * class; programmers should treat instances that are
 54  * {@linkplain #equals(Object) equal} as interchangeable and should not
 55  * use instances for synchronization, or unpredictable behavior may
 56  * occur. For example, in a future release, synchronization may fail.








 57  *
 58  * @see     java.lang.Number
 59  * @since   1.1
 60  */

 61 @jdk.internal.ValueBased
 62 public final class Byte extends Number implements Comparable<Byte>, Constable {
 63 
 64     /**
 65      * A constant holding the minimum value a {@code byte} can
 66      * have, -2<sup>7</sup>.
 67      */
 68     public static final byte   MIN_VALUE = -128;
 69 
 70     /**
 71      * A constant holding the maximum value a {@code byte} can
 72      * have, 2<sup>7</sup>-1.
 73      */
 74     public static final byte   MAX_VALUE = 127;
 75 
 76     /**
 77      * The {@code Class} instance representing the primitive type
 78      * {@code byte}.
 79      */
 80     public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");

114             final int size = -(-128) + 127 + 1;
115 
116             // Load and use the archived cache if it exists
117             CDS.initializeFromArchive(ByteCache.class);
118             if (archivedCache == null) {
119                 Byte[] c = new Byte[size];
120                 byte value = (byte)-128;
121                 for(int i = 0; i < size; i++) {
122                     c[i] = new Byte(value++);
123                 }
124                 archivedCache = c;
125             }
126             cache = archivedCache;
127             assert cache.length == size;
128         }
129     }
130 
131     /**
132      * Returns a {@code Byte} instance representing the specified
133      * {@code byte} value.
134      * If a new {@code Byte} instance is not required, this method
135      * should generally be used in preference to the constructor
136      * {@link #Byte(byte)}, as this method is likely to yield
137      * significantly better space and time performance since
138      * all byte values are cached.












139      *
140      * @param  b a byte value.
141      * @return a {@code Byte} instance representing {@code b}.
142      * @since  1.5
143      */
144     @IntrinsicCandidate

145     public static Byte valueOf(byte b) {
146         final int offset = 128;
147         return ByteCache.cache[(int)b + offset];
148     }
149 
150     /**
151      * Parses the string argument as a signed {@code byte} in the
152      * radix specified by the second argument. The characters in the
153      * string must all be digits, of the specified radix (as
154      * determined by whether {@link java.lang.Character#digit(char,
155      * int)} returns a nonnegative value) except that the first
156      * character may be an ASCII minus sign {@code '-'}
157      * ({@code '\u005Cu002D'}) to indicate a negative value or an
158      * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
159      * indicate a positive value.  The resulting {@code byte} value is
160      * returned.
161      *
162      * <p>An exception of type {@code NumberFormatException} is
163      * thrown if any of the following situations occurs:
164      * <ul>
165      * <li> The first argument is {@code null} or is a string of
166      * length zero.
167      *

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang;
 27 
 28 import jdk.internal.misc.CDS;
 29 import jdk.internal.misc.PreviewFeatures;
 30 import jdk.internal.value.DeserializeConstructor;
 31 import jdk.internal.vm.annotation.IntrinsicCandidate;
 32 import jdk.internal.vm.annotation.Stable;
 33 
 34 import java.lang.constant.Constable;
 35 import java.lang.constant.DynamicConstantDesc;
 36 import java.util.Optional;
 37 
 38 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
 39 import static java.lang.constant.ConstantDescs.CD_byte;

 40 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
 41 
 42 /**
 43  * The {@code Byte} class is the {@linkplain
 44  * java.lang##wrapperClass wrapper class} for values of the primitive
 45  * type {@code byte}. An object of type {@code Byte} contains a
 46  * single field whose type is {@code byte}.
 47  *
 48  * <p>In addition, this class provides several methods for converting
 49  * a {@code byte} to a {@code String} and a {@code String} to a {@code
 50  * byte}, as well as other constants and methods useful when dealing
 51  * with a {@code byte}.
 52  *
 53  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 54  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
 55  * as interchangeable and should not use instances for synchronization, mutexes, or
 56  * with {@linkplain java.lang.ref.Reference object references}.
 57  *
 58  * <div class="preview-block">
 59  *      <div class="preview-comment">
 60  *          When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
 61  *          Use of value class instances for synchronization, mutexes, or with
 62  *          {@linkplain java.lang.ref.Reference object references} result in
 63  *          {@link IdentityException}.
 64  *      </div>
 65  * </div>
 66  *
 67  * @see     java.lang.Number
 68  * @since   1.1
 69  */
 70 @jdk.internal.MigratedValueClass
 71 @jdk.internal.ValueBased
 72 public final class Byte extends Number implements Comparable<Byte>, Constable {
 73 
 74     /**
 75      * A constant holding the minimum value a {@code byte} can
 76      * have, -2<sup>7</sup>.
 77      */
 78     public static final byte   MIN_VALUE = -128;
 79 
 80     /**
 81      * A constant holding the maximum value a {@code byte} can
 82      * have, 2<sup>7</sup>-1.
 83      */
 84     public static final byte   MAX_VALUE = 127;
 85 
 86     /**
 87      * The {@code Class} instance representing the primitive type
 88      * {@code byte}.
 89      */
 90     public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");

124             final int size = -(-128) + 127 + 1;
125 
126             // Load and use the archived cache if it exists
127             CDS.initializeFromArchive(ByteCache.class);
128             if (archivedCache == null) {
129                 Byte[] c = new Byte[size];
130                 byte value = (byte)-128;
131                 for(int i = 0; i < size; i++) {
132                     c[i] = new Byte(value++);
133                 }
134                 archivedCache = c;
135             }
136             cache = archivedCache;
137             assert cache.length == size;
138         }
139     }
140 
141     /**
142      * Returns a {@code Byte} instance representing the specified
143      * {@code byte} value.
144      * <div class="preview-block">
145      *      <div class="preview-comment">
146      *          <p>
147      *              - When preview features are NOT enabled, {@code Byte} is an identity class.
148      *              If a new {@code Byte} instance is not required, this method
149      *              should generally be used in preference to the constructor
150      *              {@link #Byte(byte)}, as this method is likely to yield
151      *              significantly better space and time performance since
152      *              all byte values are cached.
153      *          </p>
154      *          <p>
155      *              - When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
156      *              The {@code valueOf} behavior is the same as invoking the constructor,
157      *              whether cached or not.
158      *          </p>
159      *      </div>
160      * </div>
161      *
162      * @param  b a byte value.
163      * @return a {@code Byte} instance representing {@code b}.
164      * @since  1.5
165      */
166     @IntrinsicCandidate
167     @DeserializeConstructor
168     public static Byte valueOf(byte b) {
169         final int offset = 128;
170         return (!PreviewFeatures.isEnabled()) ? ByteCache.cache[(int)b + offset] : new Byte(b);
171     }
172 
173     /**
174      * Parses the string argument as a signed {@code byte} in the
175      * radix specified by the second argument. The characters in the
176      * string must all be digits, of the specified radix (as
177      * determined by whether {@link java.lang.Character#digit(char,
178      * int)} returns a nonnegative value) except that the first
179      * character may be an ASCII minus sign {@code '-'}
180      * ({@code '\u005Cu002D'}) to indicate a negative value or an
181      * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
182      * indicate a positive value.  The resulting {@code byte} value is
183      * returned.
184      *
185      * <p>An exception of type {@code NumberFormatException} is
186      * thrown if any of the following situations occurs:
187      * <ul>
188      * <li> The first argument is {@code null} or is a string of
189      * length zero.
190      *
< prev index next >