< 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");

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>

  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.value.DeserializeConstructor;
 30 import jdk.internal.vm.annotation.IntrinsicCandidate;
 31 import jdk.internal.vm.annotation.Stable;
 32 
 33 import java.lang.constant.Constable;
 34 import java.lang.constant.DynamicConstantDesc;
 35 import java.util.Optional;
 36 
 37 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
 38 import static java.lang.constant.ConstantDescs.CD_byte;

 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 {@linkplain #equals(Object) equal}
 54  * as interchangeable and should not use instances for synchronization, mutexes, or
 55  * with {@linkplain java.lang.ref.Reference object references}.
 56  *
 57  * <div class="preview-block">
 58  *      <div class="preview-comment">
 59  *          When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
 60  *          Use of value class instances for synchronization, mutexes, or with
 61  *          {@linkplain java.lang.ref.Reference object references} result in
 62  *          {@link IdentityException}.
 63  *      </div>
 64  * </div>
 65  *
 66  * @author  Nakul Saraiya
 67  * @author  Joseph D. Darcy
 68  * @see     java.lang.Number
 69  * @since   1.1
 70  */
 71 @jdk.internal.MigratedValueClass
 72 @jdk.internal.ValueBased
 73 public final class Byte extends Number implements Comparable<Byte>, Constable {
 74 
 75     /**
 76      * A constant holding the minimum value a {@code byte} can
 77      * have, -2<sup>7</sup>.
 78      */
 79     public static final byte   MIN_VALUE = -128;
 80 
 81     /**
 82      * A constant holding the maximum value a {@code byte} can
 83      * have, 2<sup>7</sup>-1.
 84      */
 85     public static final byte   MAX_VALUE = 127;
 86 
 87     /**
 88      * The {@code Class} instance representing the primitive type
 89      * {@code byte}.
 90      */
 91     public static final Class<Byte> TYPE = Class.getPrimitiveClass("byte");

136             }
137             cache = archivedCache;
138             assert cache.length == size;
139         }
140     }
141 
142     /**
143      * Returns a {@code Byte} instance representing the specified
144      * {@code byte} value.
145      * If a new {@code Byte} instance is not required, this method
146      * should generally be used in preference to the constructor
147      * {@link #Byte(byte)}, as this method is likely to yield
148      * significantly better space and time performance since
149      * all byte values are cached.
150      *
151      * @param  b a byte value.
152      * @return a {@code Byte} instance representing {@code b}.
153      * @since  1.5
154      */
155     @IntrinsicCandidate
156     @DeserializeConstructor
157     public static Byte valueOf(byte b) {
158         final int offset = 128;
159         return ByteCache.cache[(int)b + offset];
160     }
161 
162     /**
163      * Parses the string argument as a signed {@code byte} in the
164      * radix specified by the second argument. The characters in the
165      * string must all be digits, of the specified radix (as
166      * determined by whether {@link java.lang.Character#digit(char,
167      * int)} returns a nonnegative value) except that the first
168      * character may be an ASCII minus sign {@code '-'}
169      * ({@code '\u005Cu002D'}) to indicate a negative value or an
170      * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
171      * indicate a positive value.  The resulting {@code byte} value is
172      * returned.
173      *
174      * <p>An exception of type {@code NumberFormatException} is
175      * thrown if any of the following situations occurs:
176      * <ul>
< prev index next >