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.AOTSafeClassInitializer;
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.Arrays;
36 import java.util.HashMap;
37 import java.util.Locale;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.Optional;
41
42 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
43 import static java.lang.constant.ConstantDescs.CD_char;
44 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
45
46 /**
47 * The {@code Character} class is the {@linkplain
48 * java.lang##wrapperClass wrapper class} for values of the primitive
188 * surrogate ranges as undefined characters. For example,
189 * {@code Character.isLetter('\u005CuD840')} returns {@code false}, even though
190 * this specific value if followed by any low-surrogate value in a string
191 * would represent a letter.
192 *
193 * <li>The methods that accept an {@code int} value support all
194 * Unicode characters, including supplementary characters. For
195 * example, {@code Character.isLetter(0x2F81A)} returns
196 * {@code true} because the code point value represents a letter
197 * (a CJK ideograph).
198 * </ul>
199 *
200 * <p>In the Java SE API documentation, <em>Unicode code point</em> is
201 * used for character values in the range between U+0000 and U+10FFFF,
202 * and <em>Unicode code unit</em> is used for 16-bit
203 * {@code char} values that are code units of the <em>UTF-16</em>
204 * encoding. For more information on Unicode terminology, refer to the
205 * <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
206 *
207 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
208 * class; programmers should treat instances that are
209 * {@linkplain #equals(Object) equal} as interchangeable and should not
210 * use instances for synchronization, or unpredictable behavior may
211 * occur. For example, in a future release, synchronization may fail.
212 *
213 * @spec https://www.unicode.org/reports/tr44 Unicode Character Database
214 * @author Lee Boynton
215 * @author Guy Steele
216 * @author Akira Tanaka
217 * @author Martin Buchholz
218 * @author Ulf Zibis
219 * @since 1.0
220 */
221 @jdk.internal.ValueBased
222 public final
223 class Character implements java.io.Serializable, Comparable<Character>, Constable {
224 /**
225 * The minimum radix available for conversion to and from strings.
226 * The constant value of this field is the smallest value permitted
227 * for the radix argument in radix-conversion methods such as the
228 * {@code digit} method, the {@code forDigit} method, and the
229 * {@code toString} method of class {@code Integer}.
230 *
231 * @see Character#digit(char, int)
232 * @see Character#forDigit(int, int)
233 * @see Integer#toString(int, int)
234 * @see Integer#valueOf(String)
235 */
236 public static final int MIN_RADIX = 2;
237
238 /**
239 * The maximum radix available for conversion to and from strings.
240 * The constant value of this field is the largest value permitted
241 * for the radix argument in radix-conversion methods such as the
242 * {@code digit} method, the {@code forDigit} method, and the
243 * {@code toString} method of class {@code Integer}.
9427 static {
9428 int size = 127 + 1;
9429
9430 // Load and use the archived cache if it exists
9431 CDS.initializeFromArchive(CharacterCache.class);
9432 if (archivedCache == null) {
9433 Character[] c = new Character[size];
9434 for (int i = 0; i < size; i++) {
9435 c[i] = new Character((char) i);
9436 }
9437 archivedCache = c;
9438 }
9439 cache = archivedCache;
9440 assert cache.length == size;
9441 }
9442 }
9443
9444 /**
9445 * Returns a {@code Character} instance representing the specified
9446 * {@code char} value.
9447 * If a new {@code Character} instance is not required, this method
9448 * should generally be used in preference to the constructor
9449 * {@link #Character(char)}, as this method is likely to yield
9450 * significantly better space and time performance by caching
9451 * frequently requested values.
9452 *
9453 * This method will always cache values in the range {@code
9454 * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
9455 * cache other values outside of this range.
9456 *
9457 * @param c a char value.
9458 * @return a {@code Character} instance representing {@code c}.
9459 * @since 1.5
9460 */
9461 @IntrinsicCandidate
9462 public static Character valueOf(char c) {
9463 if (c <= 127) { // must cache
9464 return CharacterCache.cache[(int)c];
9465 }
9466 return new Character(c);
9467 }
9468
9469 /**
9470 * Returns the value of this {@code Character} object.
9471 * @return the primitive {@code char} value represented by
9472 * this object.
9473 */
9474 @IntrinsicCandidate
9475 public char charValue() {
9476 return value;
9477 }
9478
9479 /**
9480 * Returns a hash code for this {@code Character}; equal to the result
9481 * of invoking {@code charValue()}.
9482 *
9483 * @return a hash code value for this {@code Character}
9484 */
|
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.AOTSafeClassInitializer;
32 import jdk.internal.vm.annotation.IntrinsicCandidate;
33 import jdk.internal.vm.annotation.Stable;
34
35 import java.lang.constant.Constable;
36 import java.lang.constant.DynamicConstantDesc;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.Locale;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.Optional;
43
44 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
45 import static java.lang.constant.ConstantDescs.CD_char;
46 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
47
48 /**
49 * The {@code Character} class is the {@linkplain
50 * java.lang##wrapperClass wrapper class} for values of the primitive
190 * surrogate ranges as undefined characters. For example,
191 * {@code Character.isLetter('\u005CuD840')} returns {@code false}, even though
192 * this specific value if followed by any low-surrogate value in a string
193 * would represent a letter.
194 *
195 * <li>The methods that accept an {@code int} value support all
196 * Unicode characters, including supplementary characters. For
197 * example, {@code Character.isLetter(0x2F81A)} returns
198 * {@code true} because the code point value represents a letter
199 * (a CJK ideograph).
200 * </ul>
201 *
202 * <p>In the Java SE API documentation, <em>Unicode code point</em> is
203 * used for character values in the range between U+0000 and U+10FFFF,
204 * and <em>Unicode code unit</em> is used for 16-bit
205 * {@code char} values that are code units of the <em>UTF-16</em>
206 * encoding. For more information on Unicode terminology, refer to the
207 * <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
208 *
209 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
210 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
211 * as interchangeable and should not use instances for synchronization, mutexes, or
212 * with {@linkplain java.lang.ref.Reference object references}.
213 *
214 * <div class="preview-block">
215 * <div class="preview-comment">
216 * When preview features are enabled, {@code Character} is a {@linkplain Class#isValue value class}.
217 * Use of value class instances for synchronization, mutexes, or with
218 * {@linkplain java.lang.ref.Reference object references} result in
219 * {@link IdentityException}.
220 * </div>
221 * </div>
222 *
223 * @spec https://www.unicode.org/reports/tr44 Unicode Character Database
224 * @author Lee Boynton
225 * @author Guy Steele
226 * @author Akira Tanaka
227 * @author Martin Buchholz
228 * @author Ulf Zibis
229 * @since 1.0
230 */
231 @jdk.internal.MigratedValueClass
232 @jdk.internal.ValueBased
233 public final class Character implements java.io.Serializable, Comparable<Character>, Constable {
234 /**
235 * The minimum radix available for conversion to and from strings.
236 * The constant value of this field is the smallest value permitted
237 * for the radix argument in radix-conversion methods such as the
238 * {@code digit} method, the {@code forDigit} method, and the
239 * {@code toString} method of class {@code Integer}.
240 *
241 * @see Character#digit(char, int)
242 * @see Character#forDigit(int, int)
243 * @see Integer#toString(int, int)
244 * @see Integer#valueOf(String)
245 */
246 public static final int MIN_RADIX = 2;
247
248 /**
249 * The maximum radix available for conversion to and from strings.
250 * The constant value of this field is the largest value permitted
251 * for the radix argument in radix-conversion methods such as the
252 * {@code digit} method, the {@code forDigit} method, and the
253 * {@code toString} method of class {@code Integer}.
9437 static {
9438 int size = 127 + 1;
9439
9440 // Load and use the archived cache if it exists
9441 CDS.initializeFromArchive(CharacterCache.class);
9442 if (archivedCache == null) {
9443 Character[] c = new Character[size];
9444 for (int i = 0; i < size; i++) {
9445 c[i] = new Character((char) i);
9446 }
9447 archivedCache = c;
9448 }
9449 cache = archivedCache;
9450 assert cache.length == size;
9451 }
9452 }
9453
9454 /**
9455 * Returns a {@code Character} instance representing the specified
9456 * {@code char} value.
9457 * <div class="preview-block">
9458 * <div class="preview-comment">
9459 * <p>
9460 * - When preview features are NOT enabled, {@code Character} is an identity class.
9461 * If a new {@code Character} instance is not required, this method
9462 * should generally be used in preference to the constructor
9463 * {@link #Character(char)}, as this method is likely to yield
9464 * significantly better space and time performance by caching
9465 * frequently requested values.
9466 * This method will always cache values in the range {@code
9467 * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
9468 * cache other values outside of this range.
9469 * </p>
9470 * <p>
9471 * - When preview features are enabled, {@code Character} is a {@linkplain Class#isValue value class}.
9472 * The {@code valueOf} behavior is the same as invoking the constructor,
9473 * whether cached or not.
9474 * </p>
9475 * </div>
9476 * </div>
9477 *
9478 * @param c a char value.
9479 * @return a {@code Character} instance representing {@code c}.
9480 * @since 1.5
9481 */
9482 @IntrinsicCandidate
9483 @DeserializeConstructor
9484 public static Character valueOf(char c) {
9485 if (!PreviewFeatures.isEnabled()) {
9486 if (c <= 127) { // must cache
9487 return CharacterCache.cache[(int) c];
9488 }
9489 }
9490 return new Character(c);
9491 }
9492
9493 /**
9494 * Returns the value of this {@code Character} object.
9495 * @return the primitive {@code char} value represented by
9496 * this object.
9497 */
9498 @IntrinsicCandidate
9499 public char charValue() {
9500 return value;
9501 }
9502
9503 /**
9504 * Returns a hash code for this {@code Character}; equal to the result
9505 * of invoking {@code charValue()}.
9506 *
9507 * @return a hash code value for this {@code Character}
9508 */
|