< prev index next > src/java.base/share/classes/java/lang/Byte.java
Print this page
*/
package java.lang;
import jdk.internal.misc.CDS;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
import java.lang.constant.Constable;
import java.lang.constant.DynamicConstantDesc;
import java.util.Optional;
import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
import static java.lang.constant.ConstantDescs.CD_byte;
- import static java.lang.constant.ConstantDescs.CD_int;
import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
/**
* The {@code Byte} class is the {@linkplain
* java.lang##wrapperClass wrapper class} for values of the primitive
*/
package java.lang;
import jdk.internal.misc.CDS;
+ import jdk.internal.misc.PreviewFeatures;
+ import jdk.internal.value.DeserializeConstructor;
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;
import java.lang.constant.Constable;
import java.lang.constant.DynamicConstantDesc;
import java.util.Optional;
import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
import static java.lang.constant.ConstantDescs.CD_byte;
import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
/**
* The {@code Byte} class is the {@linkplain
* java.lang##wrapperClass wrapper class} for values of the primitive
* a {@code byte} to a {@code String} and a {@code String} to a {@code
* byte}, as well as other constants and methods useful when dealing
* with a {@code byte}.
*
* <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
! * class; programmers should treat instances that are
! * {@linkplain #equals(Object) equal} as interchangeable and should not
! * use instances for synchronization, or unpredictable behavior may
! * occur. For example, in a future release, synchronization may fail.
*
* @author Nakul Saraiya
* @author Joseph D. Darcy
* @see java.lang.Number
* @since 1.1
*/
@jdk.internal.ValueBased
public final class Byte extends Number implements Comparable<Byte>, Constable {
/**
* A constant holding the minimum value a {@code byte} can
* a {@code byte} to a {@code String} and a {@code String} to a {@code
* byte}, as well as other constants and methods useful when dealing
* with a {@code byte}.
*
* <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
! * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
! * as interchangeable and should not use instances for synchronization, mutexes, or
! * with {@linkplain java.lang.ref.Reference object references}.
! *
+ * <div class="preview-block">
+ * <div class="preview-comment">
+ * When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
+ * Use of value class instances for synchronization, mutexes, or with
+ * {@linkplain java.lang.ref.Reference object references} result in
+ * {@link IdentityException}.
+ * </div>
+ * </div>
*
* @author Nakul Saraiya
* @author Joseph D. Darcy
* @see java.lang.Number
* @since 1.1
*/
+ @jdk.internal.MigratedValueClass
@jdk.internal.ValueBased
public final class Byte extends Number implements Comparable<Byte>, Constable {
/**
* A constant holding the minimum value a {@code byte} can
}
/**
* Returns a {@code Byte} instance representing the specified
* {@code byte} value.
! * If a new {@code Byte} instance is not required, this method
! * should generally be used in preference to the constructor
! * {@link #Byte(byte)}, as this method is likely to yield
! * significantly better space and time performance since
! * all byte values are cached.
*
* @param b a byte value.
* @return a {@code Byte} instance representing {@code b}.
* @since 1.5
*/
@IntrinsicCandidate
public static Byte valueOf(byte b) {
final int offset = 128;
! return ByteCache.cache[(int)b + offset];
}
/**
* Parses the string argument as a signed {@code byte} in the
* radix specified by the second argument. The characters in the
}
/**
* Returns a {@code Byte} instance representing the specified
* {@code byte} value.
! * <div class="preview-block">
! * <div class="preview-comment">
! * <p>
! * - When preview features are NOT enabled, {@code Byte} is an identity class.
! * If a new {@code Byte} instance is not required, this method
+ * should generally be used in preference to the constructor
+ * {@link #Byte(byte)}, as this method is likely to yield
+ * significantly better space and time performance since
+ * all byte values are cached.
+ * </p>
+ * <p>
+ * - When preview features are enabled, {@code Byte} is a {@linkplain Class#isValue value class}.
+ * The {@code valueOf} behavior is the same as invoking the constructor,
+ * whether cached or not.
+ * </p>
+ * </div>
+ * </div>
*
* @param b a byte value.
* @return a {@code Byte} instance representing {@code b}.
* @since 1.5
*/
@IntrinsicCandidate
+ @DeserializeConstructor
public static Byte valueOf(byte b) {
final int offset = 128;
! return (!PreviewFeatures.isEnabled()) ? ByteCache.cache[(int)b + offset] : new Byte(b);
}
/**
* Parses the string argument as a signed {@code byte} in the
* radix specified by the second argument. The characters in the
< prev index next >