< prev index next > src/java.base/share/classes/java/lang/Character.java
Print this page
*/
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;
* {@code char} values that are code units of the <em>UTF-16</em>
* encoding. For more information on Unicode terminology, refer to the
* <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
*
* <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.
*
* @spec https://www.unicode.org/reports/tr44 Unicode Character Database
* @author Lee Boynton
* @author Guy Steele
* @author Akira Tanaka
* @author Martin Buchholz
* @author Ulf Zibis
* @since 1.0
*/
@jdk.internal.ValueBased
! public final
- class Character implements java.io.Serializable, Comparable<Character>, Constable {
/**
* The minimum radix available for conversion to and from strings.
* The constant value of this field is the smallest value permitted
* for the radix argument in radix-conversion methods such as the
* {@code digit} method, the {@code forDigit} method, and the
* {@code char} values that are code units of the <em>UTF-16</em>
* encoding. For more information on Unicode terminology, refer to the
* <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
*
* <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 Character} 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>
*
* @spec https://www.unicode.org/reports/tr44 Unicode Character Database
* @author Lee Boynton
* @author Guy Steele
* @author Akira Tanaka
* @author Martin Buchholz
* @author Ulf Zibis
* @since 1.0
*/
+ @jdk.internal.MigratedValueClass
@jdk.internal.ValueBased
! public final class Character implements java.io.Serializable, Comparable<Character>, Constable {
/**
* The minimum radix available for conversion to and from strings.
* The constant value of this field is the smallest value permitted
* for the radix argument in radix-conversion methods such as the
* {@code digit} method, the {@code forDigit} method, and the
}
/**
* Returns a {@code Character} instance representing the specified
* {@code char} value.
! * If a new {@code Character} instance is not required, this method
! * should generally be used in preference to the constructor
! * {@link #Character(char)}, as this method is likely to yield
! * significantly better space and time performance by caching
! * frequently requested values.
! *
! * This method will always cache values in the range {@code
! * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
! * cache other values outside of this range.
*
* @param c a char value.
* @return a {@code Character} instance representing {@code c}.
* @since 1.5
*/
@IntrinsicCandidate
public static Character valueOf(char c) {
! if (c <= 127) { // must cache
! return CharacterCache.cache[(int)c];
}
return new Character(c);
}
/**
}
/**
* Returns a {@code Character} instance representing the specified
* {@code char} value.
! * <div class="preview-block">
! * <div class="preview-comment">
! * <p>
! * - When preview features are NOT enabled, {@code Character} is an identity class.
! * If a new {@code Character} instance is not required, this method
! * should generally be used in preference to the constructor
! * {@link #Character(char)}, as this method is likely to yield
! * significantly better space and time performance by caching
! * frequently requested values.
+ * This method will always cache values in the range {@code
+ * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
+ * cache other values outside of this range.
+ * </p>
+ * <p>
+ * - When preview features are enabled, {@code Character} 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 c a char value.
* @return a {@code Character} instance representing {@code c}.
* @since 1.5
*/
@IntrinsicCandidate
+ @DeserializeConstructor
public static Character valueOf(char c) {
! if (!PreviewFeatures.isEnabled()) {
! if (c <= 127) { // must cache
+ return CharacterCache.cache[(int) c];
+ }
}
return new Character(c);
}
/**
< prev index next >