< 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     @SuppressWarnings("unchecked")

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

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