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.Arrays;
35 import java.util.HashMap;
36 import java.util.Locale;
37 import java.util.Map;
38 import java.util.Objects;
39 import java.util.Optional;
40
41 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
42 import static java.lang.constant.ConstantDescs.CD_char;
43 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
44
45 /**
46 * The {@code Character} class is the {@linkplain
47 * java.lang##wrapperClass wrapper class} for values of the primitive
48 * type {@code char}. An object of type {@code Character} contains a
149 * surrogate ranges as undefined characters. For example,
150 * {@code Character.isLetter('\u005CuD840')} returns {@code false}, even though
151 * this specific value if followed by any low-surrogate value in a string
152 * would represent a letter.
153 *
154 * <li>The methods that accept an {@code int} value support all
155 * Unicode characters, including supplementary characters. For
156 * example, {@code Character.isLetter(0x2F81A)} returns
157 * {@code true} because the code point value represents a letter
158 * (a CJK ideograph).
159 * </ul>
160 *
161 * <p>In the Java SE API documentation, <em>Unicode code point</em> is
162 * used for character values in the range between U+0000 and U+10FFFF,
163 * and <em>Unicode code unit</em> is used for 16-bit
164 * {@code char} values that are code units of the <em>UTF-16</em>
165 * encoding. For more information on Unicode terminology, refer to the
166 * <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
167 *
168 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
169 * class; programmers should treat instances that are
170 * {@linkplain #equals(Object) equal} as interchangeable and should not
171 * use instances for synchronization, or unpredictable behavior may
172 * occur. For example, in a future release, synchronization may fail.
173 *
174 * @spec https://www.unicode.org/reports/tr44 Unicode Character Database
175 * @author Lee Boynton
176 * @author Guy Steele
177 * @author Akira Tanaka
178 * @author Martin Buchholz
179 * @author Ulf Zibis
180 * @since 1.0
181 */
182 @jdk.internal.ValueBased
183 public final
184 class Character implements java.io.Serializable, Comparable<Character>, Constable {
185 /**
186 * The minimum radix available for conversion to and from strings.
187 * The constant value of this field is the smallest value permitted
188 * for the radix argument in radix-conversion methods such as the
189 * {@code digit} method, the {@code forDigit} method, and the
190 * {@code toString} method of class {@code Integer}.
191 *
192 * @see Character#digit(char, int)
193 * @see Character#forDigit(int, int)
194 * @see Integer#toString(int, int)
195 * @see Integer#valueOf(String)
196 */
197 public static final int MIN_RADIX = 2;
198
199 /**
200 * The maximum radix available for conversion to and from strings.
201 * The constant value of this field is the largest value permitted
202 * for the radix argument in radix-conversion methods such as the
203 * {@code digit} method, the {@code forDigit} method, and the
204 * {@code toString} method of class {@code Integer}.
9245 static {
9246 int size = 127 + 1;
9247
9248 // Load and use the archived cache if it exists
9249 CDS.initializeFromArchive(CharacterCache.class);
9250 if (archivedCache == null) {
9251 Character[] c = new Character[size];
9252 for (int i = 0; i < size; i++) {
9253 c[i] = new Character((char) i);
9254 }
9255 archivedCache = c;
9256 }
9257 cache = archivedCache;
9258 assert cache.length == size;
9259 }
9260 }
9261
9262 /**
9263 * Returns a {@code Character} instance representing the specified
9264 * {@code char} value.
9265 * If a new {@code Character} instance is not required, this method
9266 * should generally be used in preference to the constructor
9267 * {@link #Character(char)}, as this method is likely to yield
9268 * significantly better space and time performance by caching
9269 * frequently requested values.
9270 *
9271 * This method will always cache values in the range {@code
9272 * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
9273 * cache other values outside of this range.
9274 *
9275 * @param c a char value.
9276 * @return a {@code Character} instance representing {@code c}.
9277 * @since 1.5
9278 */
9279 @IntrinsicCandidate
9280 public static Character valueOf(char c) {
9281 if (c <= 127) { // must cache
9282 return CharacterCache.cache[(int)c];
9283 }
9284 return new Character(c);
9285 }
9286
9287 /**
9288 * Returns the value of this {@code Character} object.
9289 * @return the primitive {@code char} value represented by
9290 * this object.
9291 */
9292 @IntrinsicCandidate
9293 public char charValue() {
9294 return value;
9295 }
9296
9297 /**
9298 * Returns a hash code for this {@code Character}; equal to the result
9299 * of invoking {@code charValue()}.
|
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.IntrinsicCandidate;
32 import jdk.internal.vm.annotation.Stable;
33
34 import java.lang.constant.Constable;
35 import java.lang.constant.DynamicConstantDesc;
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.Locale;
39 import java.util.Map;
40 import java.util.Objects;
41 import java.util.Optional;
42
43 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
44 import static java.lang.constant.ConstantDescs.CD_char;
45 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
46
47 /**
48 * The {@code Character} class is the {@linkplain
49 * java.lang##wrapperClass wrapper class} for values of the primitive
50 * type {@code char}. An object of type {@code Character} contains a
151 * surrogate ranges as undefined characters. For example,
152 * {@code Character.isLetter('\u005CuD840')} returns {@code false}, even though
153 * this specific value if followed by any low-surrogate value in a string
154 * would represent a letter.
155 *
156 * <li>The methods that accept an {@code int} value support all
157 * Unicode characters, including supplementary characters. For
158 * example, {@code Character.isLetter(0x2F81A)} returns
159 * {@code true} because the code point value represents a letter
160 * (a CJK ideograph).
161 * </ul>
162 *
163 * <p>In the Java SE API documentation, <em>Unicode code point</em> is
164 * used for character values in the range between U+0000 and U+10FFFF,
165 * and <em>Unicode code unit</em> is used for 16-bit
166 * {@code char} values that are code units of the <em>UTF-16</em>
167 * encoding. For more information on Unicode terminology, refer to the
168 * <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
169 *
170 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
171 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
172 * as interchangeable and should not use instances for synchronization, mutexes, or
173 * with {@linkplain java.lang.ref.Reference object references}.
174 *
175 * <div class="preview-block">
176 * <div class="preview-comment">
177 * When preview features are enabled, {@code Character} is a {@linkplain Class#isValue value class}.
178 * Use of value class instances for synchronization, mutexes, or with
179 * {@linkplain java.lang.ref.Reference object references} result in
180 * {@link IdentityException}.
181 * </div>
182 * </div>
183 *
184 * @spec https://www.unicode.org/reports/tr44 Unicode Character Database
185 * @author Lee Boynton
186 * @author Guy Steele
187 * @author Akira Tanaka
188 * @author Martin Buchholz
189 * @author Ulf Zibis
190 * @since 1.0
191 */
192 @jdk.internal.MigratedValueClass
193 @jdk.internal.ValueBased
194 public final class Character implements java.io.Serializable, Comparable<Character>, Constable {
195 /**
196 * The minimum radix available for conversion to and from strings.
197 * The constant value of this field is the smallest value permitted
198 * for the radix argument in radix-conversion methods such as the
199 * {@code digit} method, the {@code forDigit} method, and the
200 * {@code toString} method of class {@code Integer}.
201 *
202 * @see Character#digit(char, int)
203 * @see Character#forDigit(int, int)
204 * @see Integer#toString(int, int)
205 * @see Integer#valueOf(String)
206 */
207 public static final int MIN_RADIX = 2;
208
209 /**
210 * The maximum radix available for conversion to and from strings.
211 * The constant value of this field is the largest value permitted
212 * for the radix argument in radix-conversion methods such as the
213 * {@code digit} method, the {@code forDigit} method, and the
214 * {@code toString} method of class {@code Integer}.
9255 static {
9256 int size = 127 + 1;
9257
9258 // Load and use the archived cache if it exists
9259 CDS.initializeFromArchive(CharacterCache.class);
9260 if (archivedCache == null) {
9261 Character[] c = new Character[size];
9262 for (int i = 0; i < size; i++) {
9263 c[i] = new Character((char) i);
9264 }
9265 archivedCache = c;
9266 }
9267 cache = archivedCache;
9268 assert cache.length == size;
9269 }
9270 }
9271
9272 /**
9273 * Returns a {@code Character} instance representing the specified
9274 * {@code char} value.
9275 * <div class="preview-block">
9276 * <div class="preview-comment">
9277 * <p>
9278 * - When preview features are NOT enabled, {@code Character} is an identity class.
9279 * If a new {@code Character} instance is not required, this method
9280 * should generally be used in preference to the constructor
9281 * {@link #Character(char)}, as this method is likely to yield
9282 * significantly better space and time performance by caching
9283 * frequently requested values.
9284 * This method will always cache values in the range {@code
9285 * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
9286 * cache other values outside of this range.
9287 * </p>
9288 * <p>
9289 * - When preview features are enabled, {@code Character} is a {@linkplain Class#isValue value class}.
9290 * The {@code valueOf} behavior is the same as invoking the constructor,
9291 * whether cached or not.
9292 * </p>
9293 * </div>
9294 * </div>
9295 *
9296 * @param c a char value.
9297 * @return a {@code Character} instance representing {@code c}.
9298 * @since 1.5
9299 */
9300 @IntrinsicCandidate
9301 @DeserializeConstructor
9302 public static Character valueOf(char c) {
9303 if (c <= 127) { // must cache
9304 return CharacterCache.cache[(int)c];
9305 }
9306 return new Character(c);
9307 }
9308
9309 /**
9310 * Returns the value of this {@code Character} object.
9311 * @return the primitive {@code char} value represented by
9312 * this object.
9313 */
9314 @IntrinsicCandidate
9315 public char charValue() {
9316 return value;
9317 }
9318
9319 /**
9320 * Returns a hash code for this {@code Character}; equal to the result
9321 * of invoking {@code charValue()}.
|