< 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  * @author  Nakul Saraiya
 59  * @author  Joseph D. Darcy
 60  * @see     java.lang.Number
 61  * @since   1.1
 62  */

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

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












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

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

  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  * @author  Nakul Saraiya
 68  * @author  Joseph D. Darcy
 69  * @see     java.lang.Number
 70  * @since   1.1
 71  */
 72 @jdk.internal.MigratedValueClass
 73 @jdk.internal.ValueBased
 74 public final class Byte extends Number implements Comparable<Byte>, Constable {
 75 
 76     /**
 77      * A constant holding the minimum value a {@code byte} can
 78      * have, -2<sup>7</sup>.
 79      */
 80     public static final byte   MIN_VALUE = -128;
 81 
 82     /**
 83      * A constant holding the maximum value a {@code byte} can
 84      * have, 2<sup>7</sup>-1.
 85      */
 86     public static final byte   MAX_VALUE = 127;
 87 
 88     /**
 89      * The {@code Class} instance representing the primitive type
 90      * {@code byte}.
 91      */
 92     public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");

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