2 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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).
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.
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
28 import org.apidesign.bck2brwsr.core.JavaScriptBody;
31 * The {@code Character} class wraps a value of the primitive
32 * type {@code char} in an object. An object of type
33 * {@code Character} contains a single field whose type is
36 * In addition, this class provides several methods for determining
37 * a character's category (lowercase letter, digit, etc.) and for converting
38 * characters from uppercase to lowercase and vice versa.
40 * Character information is based on the Unicode Standard, version 6.0.0.
42 * The methods and data of class {@code Character} are defined by
43 * the information in the <i>UnicodeData</i> file that is part of the
44 * Unicode Character Database maintained by the Unicode
45 * Consortium. This file specifies various properties including name
46 * and general category for every defined Unicode code point or
49 * The file and its description are available from the Unicode Consortium at:
51 * <li><a href="http://www.unicode.org">http://www.unicode.org</a>
54 * <h4><a name="unicode">Unicode Character Representations</a></h4>
56 * <p>The {@code char} data type (and therefore the value that a
57 * {@code Character} object encapsulates) are based on the
58 * original Unicode specification, which defined characters as
59 * fixed-width 16-bit entities. The Unicode Standard has since been
60 * changed to allow for characters whose representation requires more
61 * than 16 bits. The range of legal <em>code point</em>s is now
62 * U+0000 to U+10FFFF, known as <em>Unicode scalar value</em>.
64 * href="http://www.unicode.org/reports/tr27/#notation"><i>
65 * definition</i></a> of the U+<i>n</i> notation in the Unicode
68 * <p><a name="BMP">The set of characters from U+0000 to U+FFFF is
69 * sometimes referred to as the <em>Basic Multilingual Plane (BMP)</em>.
70 * <a name="supplementary">Characters</a> whose code points are greater
71 * than U+FFFF are called <em>supplementary character</em>s. The Java
72 * platform uses the UTF-16 representation in {@code char} arrays and
73 * in the {@code String} and {@code StringBuffer} classes. In
74 * this representation, supplementary characters are represented as a pair
75 * of {@code char} values, the first from the <em>high-surrogates</em>
76 * range, (\uD800-\uDBFF), the second from the
77 * <em>low-surrogates</em> range (\uDC00-\uDFFF).
79 * <p>A {@code char} value, therefore, represents Basic
80 * Multilingual Plane (BMP) code points, including the surrogate
81 * code points, or code units of the UTF-16 encoding. An
82 * {@code int} value represents all Unicode code points,
83 * including supplementary code points. The lower (least significant)
84 * 21 bits of {@code int} are used to represent Unicode code
85 * points and the upper (most significant) 11 bits must be zero.
86 * Unless otherwise specified, the behavior with respect to
87 * supplementary characters and surrogate {@code char} values is
91 * <li>The methods that only accept a {@code char} value cannot support
92 * supplementary characters. They treat {@code char} values from the
93 * surrogate ranges as undefined characters. For example,
94 * {@code Character.isLetter('\u005CuD840')} returns {@code false}, even though
95 * this specific value if followed by any low-surrogate value in a string
96 * would represent a letter.
98 * <li>The methods that accept an {@code int} value support all
99 * Unicode characters, including supplementary characters. For
100 * example, {@code Character.isLetter(0x2F81A)} returns
101 * {@code true} because the code point value represents a letter
105 * <p>In the Java SE API documentation, <em>Unicode code point</em> is
106 * used for character values in the range between U+0000 and U+10FFFF,
107 * and <em>Unicode code unit</em> is used for 16-bit
108 * {@code char} values that are code units of the <em>UTF-16</em>
109 * encoding. For more information on Unicode terminology, refer to the
110 * <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
112 * @author Lee Boynton
114 * @author Akira Tanaka
115 * @author Martin Buchholz
120 class Character implements java.io.Serializable, Comparable<Character> {
122 * The minimum radix available for conversion to and from strings.
123 * The constant value of this field is the smallest value permitted
124 * for the radix argument in radix-conversion methods such as the
125 * {@code digit} method, the {@code forDigit} method, and the
126 * {@code toString} method of class {@code Integer}.
128 * @see Character#digit(char, int)
129 * @see Character#forDigit(int, int)
130 * @see Integer#toString(int, int)
131 * @see Integer#valueOf(String)
133 public static final int MIN_RADIX = 2;
136 * The maximum radix available for conversion to and from strings.
137 * The constant value of this field is the largest value permitted
138 * for the radix argument in radix-conversion methods such as the
139 * {@code digit} method, the {@code forDigit} method, and the
140 * {@code toString} method of class {@code Integer}.
142 * @see Character#digit(char, int)
143 * @see Character#forDigit(int, int)
144 * @see Integer#toString(int, int)
145 * @see Integer#valueOf(String)
147 public static final int MAX_RADIX = 36;
150 * The constant value of this field is the smallest value of type
151 * {@code char}, {@code '\u005Cu0000'}.
155 public static final char MIN_VALUE = '\u0000';
158 * The constant value of this field is the largest value of type
159 * {@code char}, {@code '\u005CuFFFF'}.
163 public static final char MAX_VALUE = '\uFFFF';
166 * The {@code Class} instance representing the primitive type
171 public static final Class<Character> TYPE = Class.getPrimitiveClass("char");
174 * Normative general types
178 * General character types
182 * General category "Cn" in the Unicode specification.
185 public static final byte UNASSIGNED = 0;
188 * General category "Lu" in the Unicode specification.
191 public static final byte UPPERCASE_LETTER = 1;
194 * General category "Ll" in the Unicode specification.
197 public static final byte LOWERCASE_LETTER = 2;
200 * General category "Lt" in the Unicode specification.
203 public static final byte TITLECASE_LETTER = 3;
206 * General category "Lm" in the Unicode specification.
209 public static final byte MODIFIER_LETTER = 4;
212 * General category "Lo" in the Unicode specification.
215 public static final byte OTHER_LETTER = 5;
218 * General category "Mn" in the Unicode specification.
221 public static final byte NON_SPACING_MARK = 6;
224 * General category "Me" in the Unicode specification.
227 public static final byte ENCLOSING_MARK = 7;
230 * General category "Mc" in the Unicode specification.
233 public static final byte COMBINING_SPACING_MARK = 8;
236 * General category "Nd" in the Unicode specification.
239 public static final byte DECIMAL_DIGIT_NUMBER = 9;
242 * General category "Nl" in the Unicode specification.
245 public static final byte LETTER_NUMBER = 10;
248 * General category "No" in the Unicode specification.
251 public static final byte OTHER_NUMBER = 11;
254 * General category "Zs" in the Unicode specification.
257 public static final byte SPACE_SEPARATOR = 12;
260 * General category "Zl" in the Unicode specification.
263 public static final byte LINE_SEPARATOR = 13;
266 * General category "Zp" in the Unicode specification.
269 public static final byte PARAGRAPH_SEPARATOR = 14;
272 * General category "Cc" in the Unicode specification.
275 public static final byte CONTROL = 15;
278 * General category "Cf" in the Unicode specification.
281 public static final byte FORMAT = 16;
284 * General category "Co" in the Unicode specification.
287 public static final byte PRIVATE_USE = 18;
290 * General category "Cs" in the Unicode specification.
293 public static final byte SURROGATE = 19;
296 * General category "Pd" in the Unicode specification.
299 public static final byte DASH_PUNCTUATION = 20;
302 * General category "Ps" in the Unicode specification.
305 public static final byte START_PUNCTUATION = 21;
308 * General category "Pe" in the Unicode specification.
311 public static final byte END_PUNCTUATION = 22;
314 * General category "Pc" in the Unicode specification.
317 public static final byte CONNECTOR_PUNCTUATION = 23;
320 * General category "Po" in the Unicode specification.
323 public static final byte OTHER_PUNCTUATION = 24;
326 * General category "Sm" in the Unicode specification.
329 public static final byte MATH_SYMBOL = 25;
332 * General category "Sc" in the Unicode specification.
335 public static final byte CURRENCY_SYMBOL = 26;
338 * General category "Sk" in the Unicode specification.
341 public static final byte MODIFIER_SYMBOL = 27;
344 * General category "So" in the Unicode specification.
347 public static final byte OTHER_SYMBOL = 28;
350 * General category "Pi" in the Unicode specification.
353 public static final byte INITIAL_QUOTE_PUNCTUATION = 29;
356 * General category "Pf" in the Unicode specification.
359 public static final byte FINAL_QUOTE_PUNCTUATION = 30;
362 * Error flag. Use int (code point) to avoid confusion with U+FFFF.
364 static final int ERROR = 0xFFFFFFFF;
368 * Undefined bidirectional character type. Undefined {@code char}
369 * values have undefined directionality in the Unicode specification.
372 public static final byte DIRECTIONALITY_UNDEFINED = -1;
375 * Strong bidirectional character type "L" in the Unicode specification.
378 public static final byte DIRECTIONALITY_LEFT_TO_RIGHT = 0;
381 * Strong bidirectional character type "R" in the Unicode specification.
384 public static final byte DIRECTIONALITY_RIGHT_TO_LEFT = 1;
387 * Strong bidirectional character type "AL" in the Unicode specification.
390 public static final byte DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC = 2;
393 * Weak bidirectional character type "EN" in the Unicode specification.
396 public static final byte DIRECTIONALITY_EUROPEAN_NUMBER = 3;
399 * Weak bidirectional character type "ES" in the Unicode specification.
402 public static final byte DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR = 4;
405 * Weak bidirectional character type "ET" in the Unicode specification.
408 public static final byte DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR = 5;
411 * Weak bidirectional character type "AN" in the Unicode specification.
414 public static final byte DIRECTIONALITY_ARABIC_NUMBER = 6;
417 * Weak bidirectional character type "CS" in the Unicode specification.
420 public static final byte DIRECTIONALITY_COMMON_NUMBER_SEPARATOR = 7;
423 * Weak bidirectional character type "NSM" in the Unicode specification.
426 public static final byte DIRECTIONALITY_NONSPACING_MARK = 8;
429 * Weak bidirectional character type "BN" in the Unicode specification.
432 public static final byte DIRECTIONALITY_BOUNDARY_NEUTRAL = 9;
435 * Neutral bidirectional character type "B" in the Unicode specification.
438 public static final byte DIRECTIONALITY_PARAGRAPH_SEPARATOR = 10;
441 * Neutral bidirectional character type "S" in the Unicode specification.
444 public static final byte DIRECTIONALITY_SEGMENT_SEPARATOR = 11;
447 * Neutral bidirectional character type "WS" in the Unicode specification.
450 public static final byte DIRECTIONALITY_WHITESPACE = 12;
453 * Neutral bidirectional character type "ON" in the Unicode specification.
456 public static final byte DIRECTIONALITY_OTHER_NEUTRALS = 13;
459 * Strong bidirectional character type "LRE" in the Unicode specification.
462 public static final byte DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING = 14;
465 * Strong bidirectional character type "LRO" in the Unicode specification.
468 public static final byte DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE = 15;
471 * Strong bidirectional character type "RLE" in the Unicode specification.
474 public static final byte DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING = 16;
477 * Strong bidirectional character type "RLO" in the Unicode specification.
480 public static final byte DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE = 17;
483 * Weak bidirectional character type "PDF" in the Unicode specification.
486 public static final byte DIRECTIONALITY_POP_DIRECTIONAL_FORMAT = 18;
489 * The minimum value of a
490 * <a href="http://www.unicode.org/glossary/#high_surrogate_code_unit">
491 * Unicode high-surrogate code unit</a>
492 * in the UTF-16 encoding, constant {@code '\u005CuD800'}.
493 * A high-surrogate is also known as a <i>leading-surrogate</i>.
497 public static final char MIN_HIGH_SURROGATE = '\uD800';
500 * The maximum value of a
501 * <a href="http://www.unicode.org/glossary/#high_surrogate_code_unit">
502 * Unicode high-surrogate code unit</a>
503 * in the UTF-16 encoding, constant {@code '\u005CuDBFF'}.
504 * A high-surrogate is also known as a <i>leading-surrogate</i>.
508 public static final char MAX_HIGH_SURROGATE = '\uDBFF';
511 * The minimum value of a
512 * <a href="http://www.unicode.org/glossary/#low_surrogate_code_unit">
513 * Unicode low-surrogate code unit</a>
514 * in the UTF-16 encoding, constant {@code '\u005CuDC00'}.
515 * A low-surrogate is also known as a <i>trailing-surrogate</i>.
519 public static final char MIN_LOW_SURROGATE = '\uDC00';
522 * The maximum value of a
523 * <a href="http://www.unicode.org/glossary/#low_surrogate_code_unit">
524 * Unicode low-surrogate code unit</a>
525 * in the UTF-16 encoding, constant {@code '\u005CuDFFF'}.
526 * A low-surrogate is also known as a <i>trailing-surrogate</i>.
530 public static final char MAX_LOW_SURROGATE = '\uDFFF';
533 * The minimum value of a Unicode surrogate code unit in the
534 * UTF-16 encoding, constant {@code '\u005CuD800'}.
538 public static final char MIN_SURROGATE = MIN_HIGH_SURROGATE;
541 * The maximum value of a Unicode surrogate code unit in the
542 * UTF-16 encoding, constant {@code '\u005CuDFFF'}.
546 public static final char MAX_SURROGATE = MAX_LOW_SURROGATE;
549 * The minimum value of a
550 * <a href="http://www.unicode.org/glossary/#supplementary_code_point">
551 * Unicode supplementary code point</a>, constant {@code U+10000}.
555 public static final int MIN_SUPPLEMENTARY_CODE_POINT = 0x010000;
558 * The minimum value of a
559 * <a href="http://www.unicode.org/glossary/#code_point">
560 * Unicode code point</a>, constant {@code U+0000}.
564 public static final int MIN_CODE_POINT = 0x000000;
567 * The maximum value of a
568 * <a href="http://www.unicode.org/glossary/#code_point">
569 * Unicode code point</a>, constant {@code U+10FFFF}.
573 public static final int MAX_CODE_POINT = 0X10FFFF;
575 public static boolean isAlphabetic(int ch) {
576 throw new UnsupportedOperationException("isAlphabetic: " + (char)ch);
579 public static boolean isIdeographic(int ch) {
580 throw new UnsupportedOperationException("isIdeographic: " + (char)ch);
583 public static boolean isLowerCase(int ch) {
584 throw new UnsupportedOperationException("isLowerCase: " + (char)ch);
587 public static boolean isUpperCase(int ch) {
588 throw new UnsupportedOperationException("isUpperCase: " + (char)ch);
591 public static boolean isMirrored(int ch) {
592 throw new UnsupportedOperationException("isMirrored: " + (char)ch);
595 public static boolean isIdentifierIgnorable(int ch) {
596 throw new UnsupportedOperationException("isIdentifierIgnorable: " + (char)ch);
599 public static boolean isUnicodeIdentifierPart(int ch) {
600 throw new UnsupportedOperationException("isUnicodeIdentifierPart: " + (char)ch);
603 public static boolean isUnicodeIdentifierStart(int ch) {
604 throw new UnsupportedOperationException("isUnicodeIdentifierStart: " + (char)ch);
607 public static char toUpperCase(int ch) {
608 throw new UnsupportedOperationException("toUpperCase: " + (char)ch);
611 public static int toLowerCase(int ch) {
612 throw new UnsupportedOperationException("toLowerCase: " + (char)ch);
617 * Instances of this class represent particular subsets of the Unicode
618 * character set. The only family of subsets defined in the
619 * {@code Character} class is {@link Character.UnicodeBlock}.
620 * Other portions of the Java API may define other subsets for their
625 public static class Subset {
630 * Constructs a new {@code Subset} instance.
632 * @param name The name of this subset
633 * @exception NullPointerException if name is {@code null}
635 protected Subset(String name) {
637 throw new NullPointerException("name");
643 * Compares two {@code Subset} objects for equality.
644 * This method returns {@code true} if and only if
645 * {@code this} and the argument refer to the same
646 * object; since this method is {@code final}, this
647 * guarantee holds for all subclasses.
649 public final boolean equals(Object obj) {
650 return (this == obj);
654 * Returns the standard hash code as defined by the
655 * {@link Object#hashCode} method. This method
656 * is {@code final} in order to ensure that the
657 * {@code equals} and {@code hashCode} methods will
658 * be consistent in all subclasses.
660 public final int hashCode() {
661 return super.hashCode();
665 * Returns the name of this subset.
667 public final String toString() {
672 // See http://www.unicode.org/Public/UNIDATA/Blocks.txt
673 // for the latest specification of Unicode Blocks.
677 * The value of the {@code Character}.
681 private final char value;
683 /** use serialVersionUID from JDK 1.0.2 for interoperability */
684 private static final long serialVersionUID = 3786198910865385080L;
687 * Constructs a newly allocated {@code Character} object that
688 * represents the specified {@code char} value.
690 * @param value the value to be represented by the
691 * {@code Character} object.
693 public Character(char value) {
697 private static class CharacterCache {
698 private CharacterCache(){}
700 static final Character cache[] = new Character[127 + 1];
703 for (int i = 0; i < cache.length; i++)
704 cache[i] = new Character((char)i);
709 * Returns a <tt>Character</tt> instance representing the specified
710 * <tt>char</tt> value.
711 * If a new <tt>Character</tt> instance is not required, this method
712 * should generally be used in preference to the constructor
713 * {@link #Character(char)}, as this method is likely to yield
714 * significantly better space and time performance by caching
715 * frequently requested values.
717 * This method will always cache values in the range {@code
718 * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
719 * cache other values outside of this range.
721 * @param c a char value.
722 * @return a <tt>Character</tt> instance representing <tt>c</tt>.
725 public static Character valueOf(char c) {
726 if (c <= 127) { // must cache
727 return CharacterCache.cache[(int)c];
729 return new Character(c);
733 * Returns the value of this {@code Character} object.
734 * @return the primitive {@code char} value represented by
737 public char charValue() {
742 * Returns a hash code for this {@code Character}; equal to the result
743 * of invoking {@code charValue()}.
745 * @return a hash code value for this {@code Character}
747 public int hashCode() {
752 * Compares this object against the specified object.
753 * The result is {@code true} if and only if the argument is not
754 * {@code null} and is a {@code Character} object that
755 * represents the same {@code char} value as this object.
757 * @param obj the object to compare with.
758 * @return {@code true} if the objects are the same;
759 * {@code false} otherwise.
761 public boolean equals(Object obj) {
762 if (obj instanceof Character) {
763 return value == ((Character)obj).charValue();
769 * Returns a {@code String} object representing this
770 * {@code Character}'s value. The result is a string of
771 * length 1 whose sole component is the primitive
772 * {@code char} value represented by this
773 * {@code Character} object.
775 * @return a string representation of this object.
777 public String toString() {
778 char buf[] = {value};
779 return String.valueOf(buf);
783 * Returns a {@code String} object representing the
784 * specified {@code char}. The result is a string of length
785 * 1 consisting solely of the specified {@code char}.
787 * @param c the {@code char} to be converted
788 * @return the string representation of the specified {@code char}
791 public static String toString(char c) {
792 return String.valueOf(c);
796 * Determines whether the specified code point is a valid
797 * <a href="http://www.unicode.org/glossary/#code_point">
798 * Unicode code point value</a>.
800 * @param codePoint the Unicode code point to be tested
801 * @return {@code true} if the specified code point value is between
802 * {@link #MIN_CODE_POINT} and
803 * {@link #MAX_CODE_POINT} inclusive;
804 * {@code false} otherwise.
807 public static boolean isValidCodePoint(int codePoint) {
808 // Optimized form of:
809 // codePoint >= MIN_CODE_POINT && codePoint <= MAX_CODE_POINT
810 int plane = codePoint >>> 16;
811 return plane < ((MAX_CODE_POINT + 1) >>> 16);
815 * Determines whether the specified character (Unicode code point)
816 * is in the <a href="#BMP">Basic Multilingual Plane (BMP)</a>.
817 * Such code points can be represented using a single {@code char}.
819 * @param codePoint the character (Unicode code point) to be tested
820 * @return {@code true} if the specified code point is between
821 * {@link #MIN_VALUE} and {@link #MAX_VALUE} inclusive;
822 * {@code false} otherwise.
825 public static boolean isBmpCodePoint(int codePoint) {
826 return codePoint >>> 16 == 0;
827 // Optimized form of:
828 // codePoint >= MIN_VALUE && codePoint <= MAX_VALUE
829 // We consistently use logical shift (>>>) to facilitate
830 // additional runtime optimizations.
834 * Determines whether the specified character (Unicode code point)
835 * is in the <a href="#supplementary">supplementary character</a> range.
837 * @param codePoint the character (Unicode code point) to be tested
838 * @return {@code true} if the specified code point is between
839 * {@link #MIN_SUPPLEMENTARY_CODE_POINT} and
840 * {@link #MAX_CODE_POINT} inclusive;
841 * {@code false} otherwise.
844 public static boolean isSupplementaryCodePoint(int codePoint) {
845 return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT
846 && codePoint < MAX_CODE_POINT + 1;
850 * Determines if the given {@code char} value is a
851 * <a href="http://www.unicode.org/glossary/#high_surrogate_code_unit">
852 * Unicode high-surrogate code unit</a>
853 * (also known as <i>leading-surrogate code unit</i>).
855 * <p>Such values do not represent characters by themselves,
856 * but are used in the representation of
857 * <a href="#supplementary">supplementary characters</a>
858 * in the UTF-16 encoding.
860 * @param ch the {@code char} value to be tested.
861 * @return {@code true} if the {@code char} value is between
862 * {@link #MIN_HIGH_SURROGATE} and
863 * {@link #MAX_HIGH_SURROGATE} inclusive;
864 * {@code false} otherwise.
865 * @see Character#isLowSurrogate(char)
866 * @see Character.UnicodeBlock#of(int)
869 public static boolean isHighSurrogate(char ch) {
870 // Help VM constant-fold; MAX_HIGH_SURROGATE + 1 == MIN_LOW_SURROGATE
871 return ch >= MIN_HIGH_SURROGATE && ch < (MAX_HIGH_SURROGATE + 1);
875 * Determines if the given {@code char} value is a
876 * <a href="http://www.unicode.org/glossary/#low_surrogate_code_unit">
877 * Unicode low-surrogate code unit</a>
878 * (also known as <i>trailing-surrogate code unit</i>).
880 * <p>Such values do not represent characters by themselves,
881 * but are used in the representation of
882 * <a href="#supplementary">supplementary characters</a>
883 * in the UTF-16 encoding.
885 * @param ch the {@code char} value to be tested.
886 * @return {@code true} if the {@code char} value is between
887 * {@link #MIN_LOW_SURROGATE} and
888 * {@link #MAX_LOW_SURROGATE} inclusive;
889 * {@code false} otherwise.
890 * @see Character#isHighSurrogate(char)
893 public static boolean isLowSurrogate(char ch) {
894 return ch >= MIN_LOW_SURROGATE && ch < (MAX_LOW_SURROGATE + 1);
898 * Determines if the given {@code char} value is a Unicode
899 * <i>surrogate code unit</i>.
901 * <p>Such values do not represent characters by themselves,
902 * but are used in the representation of
903 * <a href="#supplementary">supplementary characters</a>
904 * in the UTF-16 encoding.
906 * <p>A char value is a surrogate code unit if and only if it is either
907 * a {@linkplain #isLowSurrogate(char) low-surrogate code unit} or
908 * a {@linkplain #isHighSurrogate(char) high-surrogate code unit}.
910 * @param ch the {@code char} value to be tested.
911 * @return {@code true} if the {@code char} value is between
912 * {@link #MIN_SURROGATE} and
913 * {@link #MAX_SURROGATE} inclusive;
914 * {@code false} otherwise.
917 public static boolean isSurrogate(char ch) {
918 return ch >= MIN_SURROGATE && ch < (MAX_SURROGATE + 1);
922 * Determines whether the specified pair of {@code char}
924 * <a href="http://www.unicode.org/glossary/#surrogate_pair">
925 * Unicode surrogate pair</a>.
927 * <p>This method is equivalent to the expression:
929 * isHighSurrogate(high) && isLowSurrogate(low)
930 * </pre></blockquote>
932 * @param high the high-surrogate code value to be tested
933 * @param low the low-surrogate code value to be tested
934 * @return {@code true} if the specified high and
935 * low-surrogate code values represent a valid surrogate pair;
936 * {@code false} otherwise.
939 public static boolean isSurrogatePair(char high, char low) {
940 return isHighSurrogate(high) && isLowSurrogate(low);
944 * Determines the number of {@code char} values needed to
945 * represent the specified character (Unicode code point). If the
946 * specified character is equal to or greater than 0x10000, then
947 * the method returns 2. Otherwise, the method returns 1.
949 * <p>This method doesn't validate the specified character to be a
950 * valid Unicode code point. The caller must validate the
951 * character value using {@link #isValidCodePoint(int) isValidCodePoint}
954 * @param codePoint the character (Unicode code point) to be tested.
955 * @return 2 if the character is a valid supplementary character; 1 otherwise.
956 * @see Character#isSupplementaryCodePoint(int)
959 public static int charCount(int codePoint) {
960 return codePoint >= MIN_SUPPLEMENTARY_CODE_POINT ? 2 : 1;
964 * Converts the specified surrogate pair to its supplementary code
965 * point value. This method does not validate the specified
966 * surrogate pair. The caller must validate it using {@link
967 * #isSurrogatePair(char, char) isSurrogatePair} if necessary.
969 * @param high the high-surrogate code unit
970 * @param low the low-surrogate code unit
971 * @return the supplementary code point composed from the
972 * specified surrogate pair.
975 public static int toCodePoint(char high, char low) {
976 // Optimized form of:
977 // return ((high - MIN_HIGH_SURROGATE) << 10)
978 // + (low - MIN_LOW_SURROGATE)
979 // + MIN_SUPPLEMENTARY_CODE_POINT;
980 return ((high << 10) + low) + (MIN_SUPPLEMENTARY_CODE_POINT
981 - (MIN_HIGH_SURROGATE << 10)
982 - MIN_LOW_SURROGATE);
986 * Returns the code point at the given index of the
987 * {@code CharSequence}. If the {@code char} value at
988 * the given index in the {@code CharSequence} is in the
989 * high-surrogate range, the following index is less than the
990 * length of the {@code CharSequence}, and the
991 * {@code char} value at the following index is in the
992 * low-surrogate range, then the supplementary code point
993 * corresponding to this surrogate pair is returned. Otherwise,
994 * the {@code char} value at the given index is returned.
996 * @param seq a sequence of {@code char} values (Unicode code
998 * @param index the index to the {@code char} values (Unicode
999 * code units) in {@code seq} to be converted
1000 * @return the Unicode code point at the given index
1001 * @exception NullPointerException if {@code seq} is null.
1002 * @exception IndexOutOfBoundsException if the value
1003 * {@code index} is negative or not less than
1004 * {@link CharSequence#length() seq.length()}.
1007 public static int codePointAt(CharSequence seq, int index) {
1008 char c1 = seq.charAt(index++);
1009 if (isHighSurrogate(c1)) {
1010 if (index < seq.length()) {
1011 char c2 = seq.charAt(index);
1012 if (isLowSurrogate(c2)) {
1013 return toCodePoint(c1, c2);
1021 * Returns the code point at the given index of the
1022 * {@code char} array. If the {@code char} value at
1023 * the given index in the {@code char} array is in the
1024 * high-surrogate range, the following index is less than the
1025 * length of the {@code char} array, and the
1026 * {@code char} value at the following index is in the
1027 * low-surrogate range, then the supplementary code point
1028 * corresponding to this surrogate pair is returned. Otherwise,
1029 * the {@code char} value at the given index is returned.
1031 * @param a the {@code char} array
1032 * @param index the index to the {@code char} values (Unicode
1033 * code units) in the {@code char} array to be converted
1034 * @return the Unicode code point at the given index
1035 * @exception NullPointerException if {@code a} is null.
1036 * @exception IndexOutOfBoundsException if the value
1037 * {@code index} is negative or not less than
1038 * the length of the {@code char} array.
1041 public static int codePointAt(char[] a, int index) {
1042 return codePointAtImpl(a, index, a.length);
1046 * Returns the code point at the given index of the
1047 * {@code char} array, where only array elements with
1048 * {@code index} less than {@code limit} can be used. If
1049 * the {@code char} value at the given index in the
1050 * {@code char} array is in the high-surrogate range, the
1051 * following index is less than the {@code limit}, and the
1052 * {@code char} value at the following index is in the
1053 * low-surrogate range, then the supplementary code point
1054 * corresponding to this surrogate pair is returned. Otherwise,
1055 * the {@code char} value at the given index is returned.
1057 * @param a the {@code char} array
1058 * @param index the index to the {@code char} values (Unicode
1059 * code units) in the {@code char} array to be converted
1060 * @param limit the index after the last array element that
1061 * can be used in the {@code char} array
1062 * @return the Unicode code point at the given index
1063 * @exception NullPointerException if {@code a} is null.
1064 * @exception IndexOutOfBoundsException if the {@code index}
1065 * argument is negative or not less than the {@code limit}
1066 * argument, or if the {@code limit} argument is negative or
1067 * greater than the length of the {@code char} array.
1070 public static int codePointAt(char[] a, int index, int limit) {
1071 if (index >= limit || limit < 0 || limit > a.length) {
1072 throw new IndexOutOfBoundsException();
1074 return codePointAtImpl(a, index, limit);
1077 // throws ArrayIndexOutofBoundsException if index out of bounds
1078 static int codePointAtImpl(char[] a, int index, int limit) {
1079 char c1 = a[index++];
1080 if (isHighSurrogate(c1)) {
1081 if (index < limit) {
1083 if (isLowSurrogate(c2)) {
1084 return toCodePoint(c1, c2);
1092 * Returns the code point preceding the given index of the
1093 * {@code CharSequence}. If the {@code char} value at
1094 * {@code (index - 1)} in the {@code CharSequence} is in
1095 * the low-surrogate range, {@code (index - 2)} is not
1096 * negative, and the {@code char} value at {@code (index - 2)}
1097 * in the {@code CharSequence} is in the
1098 * high-surrogate range, then the supplementary code point
1099 * corresponding to this surrogate pair is returned. Otherwise,
1100 * the {@code char} value at {@code (index - 1)} is
1103 * @param seq the {@code CharSequence} instance
1104 * @param index the index following the code point that should be returned
1105 * @return the Unicode code point value before the given index.
1106 * @exception NullPointerException if {@code seq} is null.
1107 * @exception IndexOutOfBoundsException if the {@code index}
1108 * argument is less than 1 or greater than {@link
1109 * CharSequence#length() seq.length()}.
1112 public static int codePointBefore(CharSequence seq, int index) {
1113 char c2 = seq.charAt(--index);
1114 if (isLowSurrogate(c2)) {
1116 char c1 = seq.charAt(--index);
1117 if (isHighSurrogate(c1)) {
1118 return toCodePoint(c1, c2);
1126 * Returns the code point preceding the given index of the
1127 * {@code char} array. If the {@code char} value at
1128 * {@code (index - 1)} in the {@code char} array is in
1129 * the low-surrogate range, {@code (index - 2)} is not
1130 * negative, and the {@code char} value at {@code (index - 2)}
1131 * in the {@code char} array is in the
1132 * high-surrogate range, then the supplementary code point
1133 * corresponding to this surrogate pair is returned. Otherwise,
1134 * the {@code char} value at {@code (index - 1)} is
1137 * @param a the {@code char} array
1138 * @param index the index following the code point that should be returned
1139 * @return the Unicode code point value before the given index.
1140 * @exception NullPointerException if {@code a} is null.
1141 * @exception IndexOutOfBoundsException if the {@code index}
1142 * argument is less than 1 or greater than the length of the
1143 * {@code char} array
1146 public static int codePointBefore(char[] a, int index) {
1147 return codePointBeforeImpl(a, index, 0);
1151 * Returns the code point preceding the given index of the
1152 * {@code char} array, where only array elements with
1153 * {@code index} greater than or equal to {@code start}
1154 * can be used. If the {@code char} value at {@code (index - 1)}
1155 * in the {@code char} array is in the
1156 * low-surrogate range, {@code (index - 2)} is not less than
1157 * {@code start}, and the {@code char} value at
1158 * {@code (index - 2)} in the {@code char} array is in
1159 * the high-surrogate range, then the supplementary code point
1160 * corresponding to this surrogate pair is returned. Otherwise,
1161 * the {@code char} value at {@code (index - 1)} is
1164 * @param a the {@code char} array
1165 * @param index the index following the code point that should be returned
1166 * @param start the index of the first array element in the
1167 * {@code char} array
1168 * @return the Unicode code point value before the given index.
1169 * @exception NullPointerException if {@code a} is null.
1170 * @exception IndexOutOfBoundsException if the {@code index}
1171 * argument is not greater than the {@code start} argument or
1172 * is greater than the length of the {@code char} array, or
1173 * if the {@code start} argument is negative or not less than
1174 * the length of the {@code char} array.
1177 public static int codePointBefore(char[] a, int index, int start) {
1178 if (index <= start || start < 0 || start >= a.length) {
1179 throw new IndexOutOfBoundsException();
1181 return codePointBeforeImpl(a, index, start);
1184 // throws ArrayIndexOutofBoundsException if index-1 out of bounds
1185 static int codePointBeforeImpl(char[] a, int index, int start) {
1186 char c2 = a[--index];
1187 if (isLowSurrogate(c2)) {
1188 if (index > start) {
1189 char c1 = a[--index];
1190 if (isHighSurrogate(c1)) {
1191 return toCodePoint(c1, c2);
1199 * Returns the leading surrogate (a
1200 * <a href="http://www.unicode.org/glossary/#high_surrogate_code_unit">
1201 * high surrogate code unit</a>) of the
1202 * <a href="http://www.unicode.org/glossary/#surrogate_pair">
1203 * surrogate pair</a>
1204 * representing the specified supplementary character (Unicode
1205 * code point) in the UTF-16 encoding. If the specified character
1207 * <a href="Character.html#supplementary">supplementary character</a>,
1208 * an unspecified {@code char} is returned.
1211 * {@link #isSupplementaryCodePoint isSupplementaryCodePoint(x)}
1212 * is {@code true}, then
1213 * {@link #isHighSurrogate isHighSurrogate}{@code (highSurrogate(x))} and
1214 * {@link #toCodePoint toCodePoint}{@code (highSurrogate(x), }{@link #lowSurrogate lowSurrogate}{@code (x)) == x}
1215 * are also always {@code true}.
1217 * @param codePoint a supplementary character (Unicode code point)
1218 * @return the leading surrogate code unit used to represent the
1219 * character in the UTF-16 encoding
1222 public static char highSurrogate(int codePoint) {
1223 return (char) ((codePoint >>> 10)
1224 + (MIN_HIGH_SURROGATE - (MIN_SUPPLEMENTARY_CODE_POINT >>> 10)));
1228 * Returns the trailing surrogate (a
1229 * <a href="http://www.unicode.org/glossary/#low_surrogate_code_unit">
1230 * low surrogate code unit</a>) of the
1231 * <a href="http://www.unicode.org/glossary/#surrogate_pair">
1232 * surrogate pair</a>
1233 * representing the specified supplementary character (Unicode
1234 * code point) in the UTF-16 encoding. If the specified character
1236 * <a href="Character.html#supplementary">supplementary character</a>,
1237 * an unspecified {@code char} is returned.
1240 * {@link #isSupplementaryCodePoint isSupplementaryCodePoint(x)}
1241 * is {@code true}, then
1242 * {@link #isLowSurrogate isLowSurrogate}{@code (lowSurrogate(x))} and
1243 * {@link #toCodePoint toCodePoint}{@code (}{@link #highSurrogate highSurrogate}{@code (x), lowSurrogate(x)) == x}
1244 * are also always {@code true}.
1246 * @param codePoint a supplementary character (Unicode code point)
1247 * @return the trailing surrogate code unit used to represent the
1248 * character in the UTF-16 encoding
1251 public static char lowSurrogate(int codePoint) {
1252 return (char) ((codePoint & 0x3ff) + MIN_LOW_SURROGATE);
1256 * Converts the specified character (Unicode code point) to its
1257 * UTF-16 representation. If the specified code point is a BMP
1258 * (Basic Multilingual Plane or Plane 0) value, the same value is
1259 * stored in {@code dst[dstIndex]}, and 1 is returned. If the
1260 * specified code point is a supplementary character, its
1261 * surrogate values are stored in {@code dst[dstIndex]}
1262 * (high-surrogate) and {@code dst[dstIndex+1]}
1263 * (low-surrogate), and 2 is returned.
1265 * @param codePoint the character (Unicode code point) to be converted.
1266 * @param dst an array of {@code char} in which the
1267 * {@code codePoint}'s UTF-16 value is stored.
1268 * @param dstIndex the start index into the {@code dst}
1269 * array where the converted value is stored.
1270 * @return 1 if the code point is a BMP code point, 2 if the
1271 * code point is a supplementary code point.
1272 * @exception IllegalArgumentException if the specified
1273 * {@code codePoint} is not a valid Unicode code point.
1274 * @exception NullPointerException if the specified {@code dst} is null.
1275 * @exception IndexOutOfBoundsException if {@code dstIndex}
1276 * is negative or not less than {@code dst.length}, or if
1277 * {@code dst} at {@code dstIndex} doesn't have enough
1278 * array element(s) to store the resulting {@code char}
1279 * value(s). (If {@code dstIndex} is equal to
1280 * {@code dst.length-1} and the specified
1281 * {@code codePoint} is a supplementary character, the
1282 * high-surrogate value is not stored in
1283 * {@code dst[dstIndex]}.)
1286 public static int toChars(int codePoint, char[] dst, int dstIndex) {
1287 if (isBmpCodePoint(codePoint)) {
1288 dst[dstIndex] = (char) codePoint;
1290 } else if (isValidCodePoint(codePoint)) {
1291 toSurrogates(codePoint, dst, dstIndex);
1294 throw new IllegalArgumentException();
1299 * Converts the specified character (Unicode code point) to its
1300 * UTF-16 representation stored in a {@code char} array. If
1301 * the specified code point is a BMP (Basic Multilingual Plane or
1302 * Plane 0) value, the resulting {@code char} array has
1303 * the same value as {@code codePoint}. If the specified code
1304 * point is a supplementary code point, the resulting
1305 * {@code char} array has the corresponding surrogate pair.
1307 * @param codePoint a Unicode code point
1308 * @return a {@code char} array having
1309 * {@code codePoint}'s UTF-16 representation.
1310 * @exception IllegalArgumentException if the specified
1311 * {@code codePoint} is not a valid Unicode code point.
1314 public static char[] toChars(int codePoint) {
1315 if (isBmpCodePoint(codePoint)) {
1316 return new char[] { (char) codePoint };
1317 } else if (isValidCodePoint(codePoint)) {
1318 char[] result = new char[2];
1319 toSurrogates(codePoint, result, 0);
1322 throw new IllegalArgumentException();
1326 static void toSurrogates(int codePoint, char[] dst, int index) {
1327 // We write elements "backwards" to guarantee all-or-nothing
1328 dst[index+1] = lowSurrogate(codePoint);
1329 dst[index] = highSurrogate(codePoint);
1333 * Returns the number of Unicode code points in the text range of
1334 * the specified char sequence. The text range begins at the
1335 * specified {@code beginIndex} and extends to the
1336 * {@code char} at index {@code endIndex - 1}. Thus the
1337 * length (in {@code char}s) of the text range is
1338 * {@code endIndex-beginIndex}. Unpaired surrogates within
1339 * the text range count as one code point each.
1341 * @param seq the char sequence
1342 * @param beginIndex the index to the first {@code char} of
1344 * @param endIndex the index after the last {@code char} of
1346 * @return the number of Unicode code points in the specified text
1348 * @exception NullPointerException if {@code seq} is null.
1349 * @exception IndexOutOfBoundsException if the
1350 * {@code beginIndex} is negative, or {@code endIndex}
1351 * is larger than the length of the given sequence, or
1352 * {@code beginIndex} is larger than {@code endIndex}.
1355 public static int codePointCount(CharSequence seq, int beginIndex, int endIndex) {
1356 int length = seq.length();
1357 if (beginIndex < 0 || endIndex > length || beginIndex > endIndex) {
1358 throw new IndexOutOfBoundsException();
1360 int n = endIndex - beginIndex;
1361 for (int i = beginIndex; i < endIndex; ) {
1362 if (isHighSurrogate(seq.charAt(i++)) && i < endIndex &&
1363 isLowSurrogate(seq.charAt(i))) {
1372 * Returns the number of Unicode code points in a subarray of the
1373 * {@code char} array argument. The {@code offset}
1374 * argument is the index of the first {@code char} of the
1375 * subarray and the {@code count} argument specifies the
1376 * length of the subarray in {@code char}s. Unpaired
1377 * surrogates within the subarray count as one code point each.
1379 * @param a the {@code char} array
1380 * @param offset the index of the first {@code char} in the
1381 * given {@code char} array
1382 * @param count the length of the subarray in {@code char}s
1383 * @return the number of Unicode code points in the specified subarray
1384 * @exception NullPointerException if {@code a} is null.
1385 * @exception IndexOutOfBoundsException if {@code offset} or
1386 * {@code count} is negative, or if {@code offset +
1387 * count} is larger than the length of the given array.
1390 public static int codePointCount(char[] a, int offset, int count) {
1391 if (count > a.length - offset || offset < 0 || count < 0) {
1392 throw new IndexOutOfBoundsException();
1394 return codePointCountImpl(a, offset, count);
1397 static int codePointCountImpl(char[] a, int offset, int count) {
1398 int endIndex = offset + count;
1400 for (int i = offset; i < endIndex; ) {
1401 if (isHighSurrogate(a[i++]) && i < endIndex &&
1402 isLowSurrogate(a[i])) {
1411 * Returns the index within the given char sequence that is offset
1412 * from the given {@code index} by {@code codePointOffset}
1413 * code points. Unpaired surrogates within the text range given by
1414 * {@code index} and {@code codePointOffset} count as
1415 * one code point each.
1417 * @param seq the char sequence
1418 * @param index the index to be offset
1419 * @param codePointOffset the offset in code points
1420 * @return the index within the char sequence
1421 * @exception NullPointerException if {@code seq} is null.
1422 * @exception IndexOutOfBoundsException if {@code index}
1423 * is negative or larger then the length of the char sequence,
1424 * or if {@code codePointOffset} is positive and the
1425 * subsequence starting with {@code index} has fewer than
1426 * {@code codePointOffset} code points, or if
1427 * {@code codePointOffset} is negative and the subsequence
1428 * before {@code index} has fewer than the absolute value
1429 * of {@code codePointOffset} code points.
1432 public static int offsetByCodePoints(CharSequence seq, int index,
1433 int codePointOffset) {
1434 int length = seq.length();
1435 if (index < 0 || index > length) {
1436 throw new IndexOutOfBoundsException();
1440 if (codePointOffset >= 0) {
1442 for (i = 0; x < length && i < codePointOffset; i++) {
1443 if (isHighSurrogate(seq.charAt(x++)) && x < length &&
1444 isLowSurrogate(seq.charAt(x))) {
1448 if (i < codePointOffset) {
1449 throw new IndexOutOfBoundsException();
1453 for (i = codePointOffset; x > 0 && i < 0; i++) {
1454 if (isLowSurrogate(seq.charAt(--x)) && x > 0 &&
1455 isHighSurrogate(seq.charAt(x-1))) {
1460 throw new IndexOutOfBoundsException();
1467 * Returns the index within the given {@code char} subarray
1468 * that is offset from the given {@code index} by
1469 * {@code codePointOffset} code points. The
1470 * {@code start} and {@code count} arguments specify a
1471 * subarray of the {@code char} array. Unpaired surrogates
1472 * within the text range given by {@code index} and
1473 * {@code codePointOffset} count as one code point each.
1475 * @param a the {@code char} array
1476 * @param start the index of the first {@code char} of the
1478 * @param count the length of the subarray in {@code char}s
1479 * @param index the index to be offset
1480 * @param codePointOffset the offset in code points
1481 * @return the index within the subarray
1482 * @exception NullPointerException if {@code a} is null.
1483 * @exception IndexOutOfBoundsException
1484 * if {@code start} or {@code count} is negative,
1485 * or if {@code start + count} is larger than the length of
1487 * or if {@code index} is less than {@code start} or
1488 * larger then {@code start + count},
1489 * or if {@code codePointOffset} is positive and the text range
1490 * starting with {@code index} and ending with {@code start + count - 1}
1491 * has fewer than {@code codePointOffset} code
1493 * or if {@code codePointOffset} is negative and the text range
1494 * starting with {@code start} and ending with {@code index - 1}
1495 * has fewer than the absolute value of
1496 * {@code codePointOffset} code points.
1499 public static int offsetByCodePoints(char[] a, int start, int count,
1500 int index, int codePointOffset) {
1501 if (count > a.length-start || start < 0 || count < 0
1502 || index < start || index > start+count) {
1503 throw new IndexOutOfBoundsException();
1505 return offsetByCodePointsImpl(a, start, count, index, codePointOffset);
1508 static int offsetByCodePointsImpl(char[]a, int start, int count,
1509 int index, int codePointOffset) {
1511 if (codePointOffset >= 0) {
1512 int limit = start + count;
1514 for (i = 0; x < limit && i < codePointOffset; i++) {
1515 if (isHighSurrogate(a[x++]) && x < limit &&
1516 isLowSurrogate(a[x])) {
1520 if (i < codePointOffset) {
1521 throw new IndexOutOfBoundsException();
1525 for (i = codePointOffset; x > start && i < 0; i++) {
1526 if (isLowSurrogate(a[--x]) && x > start &&
1527 isHighSurrogate(a[x-1])) {
1532 throw new IndexOutOfBoundsException();
1539 * Determines if the specified character is a lowercase character.
1541 * A character is lowercase if its general category type, provided
1542 * by {@code Character.getType(ch)}, is
1543 * {@code LOWERCASE_LETTER}, or it has contributory property
1544 * Other_Lowercase as defined by the Unicode Standard.
1546 * The following are examples of lowercase characters:
1547 * <p><blockquote><pre>
1548 * a b c d e f g h i j k l m n o p q r s t u v w x y z
1549 * '\u00DF' '\u00E0' '\u00E1' '\u00E2' '\u00E3' '\u00E4' '\u00E5' '\u00E6'
1550 * '\u00E7' '\u00E8' '\u00E9' '\u00EA' '\u00EB' '\u00EC' '\u00ED' '\u00EE'
1551 * '\u00EF' '\u00F0' '\u00F1' '\u00F2' '\u00F3' '\u00F4' '\u00F5' '\u00F6'
1552 * '\u00F8' '\u00F9' '\u00FA' '\u00FB' '\u00FC' '\u00FD' '\u00FE' '\u00FF'
1553 * </pre></blockquote>
1554 * <p> Many other Unicode characters are lowercase too.
1556 * <p><b>Note:</b> This method cannot handle <a
1557 * href="#supplementary"> supplementary characters</a>. To support
1558 * all Unicode characters, including supplementary characters, use
1559 * the {@link #isLowerCase(int)} method.
1561 * @param ch the character to be tested.
1562 * @return {@code true} if the character is lowercase;
1563 * {@code false} otherwise.
1564 * @see Character#isLowerCase(char)
1565 * @see Character#isTitleCase(char)
1566 * @see Character#toLowerCase(char)
1567 * @see Character#getType(char)
1569 public static boolean isLowerCase(char ch) {
1570 return ch == toLowerCase(ch);
1574 * Determines if the specified character is an uppercase character.
1576 * A character is uppercase if its general category type, provided by
1577 * {@code Character.getType(ch)}, is {@code UPPERCASE_LETTER}.
1578 * or it has contributory property Other_Uppercase as defined by the Unicode Standard.
1580 * The following are examples of uppercase characters:
1581 * <p><blockquote><pre>
1582 * A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1583 * '\u00C0' '\u00C1' '\u00C2' '\u00C3' '\u00C4' '\u00C5' '\u00C6' '\u00C7'
1584 * '\u00C8' '\u00C9' '\u00CA' '\u00CB' '\u00CC' '\u00CD' '\u00CE' '\u00CF'
1585 * '\u00D0' '\u00D1' '\u00D2' '\u00D3' '\u00D4' '\u00D5' '\u00D6' '\u00D8'
1586 * '\u00D9' '\u00DA' '\u00DB' '\u00DC' '\u00DD' '\u00DE'
1587 * </pre></blockquote>
1588 * <p> Many other Unicode characters are uppercase too.<p>
1590 * <p><b>Note:</b> This method cannot handle <a
1591 * href="#supplementary"> supplementary characters</a>. To support
1592 * all Unicode characters, including supplementary characters, use
1593 * the {@link #isUpperCase(int)} method.
1595 * @param ch the character to be tested.
1596 * @return {@code true} if the character is uppercase;
1597 * {@code false} otherwise.
1598 * @see Character#isLowerCase(char)
1599 * @see Character#isTitleCase(char)
1600 * @see Character#toUpperCase(char)
1601 * @see Character#getType(char)
1604 public static boolean isUpperCase(char ch) {
1605 return ch == toUpperCase(ch);
1609 * Determines if the specified character is a titlecase character.
1611 * A character is a titlecase character if its general
1612 * category type, provided by {@code Character.getType(ch)},
1613 * is {@code TITLECASE_LETTER}.
1615 * Some characters look like pairs of Latin letters. For example, there
1616 * is an uppercase letter that looks like "LJ" and has a corresponding
1617 * lowercase letter that looks like "lj". A third form, which looks like "Lj",
1618 * is the appropriate form to use when rendering a word in lowercase
1619 * with initial capitals, as for a book title.
1621 * These are some of the Unicode characters for which this method returns
1624 * <li>{@code LATIN CAPITAL LETTER D WITH SMALL LETTER Z WITH CARON}
1625 * <li>{@code LATIN CAPITAL LETTER L WITH SMALL LETTER J}
1626 * <li>{@code LATIN CAPITAL LETTER N WITH SMALL LETTER J}
1627 * <li>{@code LATIN CAPITAL LETTER D WITH SMALL LETTER Z}
1629 * <p> Many other Unicode characters are titlecase too.<p>
1631 * <p><b>Note:</b> This method cannot handle <a
1632 * href="#supplementary"> supplementary characters</a>. To support
1633 * all Unicode characters, including supplementary characters, use
1634 * the {@link #isTitleCase(int)} method.
1636 * @param ch the character to be tested.
1637 * @return {@code true} if the character is titlecase;
1638 * {@code false} otherwise.
1639 * @see Character#isLowerCase(char)
1640 * @see Character#isUpperCase(char)
1641 * @see Character#toTitleCase(char)
1642 * @see Character#getType(char)
1645 public static boolean isTitleCase(char ch) {
1646 return isTitleCase((int)ch);
1650 * Determines if the specified character (Unicode code point) is a titlecase character.
1652 * A character is a titlecase character if its general
1653 * category type, provided by {@link Character#getType(int) getType(codePoint)},
1654 * is {@code TITLECASE_LETTER}.
1656 * Some characters look like pairs of Latin letters. For example, there
1657 * is an uppercase letter that looks like "LJ" and has a corresponding
1658 * lowercase letter that looks like "lj". A third form, which looks like "Lj",
1659 * is the appropriate form to use when rendering a word in lowercase
1660 * with initial capitals, as for a book title.
1662 * These are some of the Unicode characters for which this method returns
1665 * <li>{@code LATIN CAPITAL LETTER D WITH SMALL LETTER Z WITH CARON}
1666 * <li>{@code LATIN CAPITAL LETTER L WITH SMALL LETTER J}
1667 * <li>{@code LATIN CAPITAL LETTER N WITH SMALL LETTER J}
1668 * <li>{@code LATIN CAPITAL LETTER D WITH SMALL LETTER Z}
1670 * <p> Many other Unicode characters are titlecase too.<p>
1672 * @param codePoint the character (Unicode code point) to be tested.
1673 * @return {@code true} if the character is titlecase;
1674 * {@code false} otherwise.
1675 * @see Character#isLowerCase(int)
1676 * @see Character#isUpperCase(int)
1677 * @see Character#toTitleCase(int)
1678 * @see Character#getType(int)
1681 public static boolean isTitleCase(int codePoint) {
1682 return getType(codePoint) == Character.TITLECASE_LETTER;
1686 * Determines if the specified character is a digit.
1688 * A character is a digit if its general category type, provided
1689 * by {@code Character.getType(ch)}, is
1690 * {@code DECIMAL_DIGIT_NUMBER}.
1692 * Some Unicode character ranges that contain digits:
1694 * <li>{@code '\u005Cu0030'} through {@code '\u005Cu0039'},
1695 * ISO-LATIN-1 digits ({@code '0'} through {@code '9'})
1696 * <li>{@code '\u005Cu0660'} through {@code '\u005Cu0669'},
1697 * Arabic-Indic digits
1698 * <li>{@code '\u005Cu06F0'} through {@code '\u005Cu06F9'},
1699 * Extended Arabic-Indic digits
1700 * <li>{@code '\u005Cu0966'} through {@code '\u005Cu096F'},
1702 * <li>{@code '\u005CuFF10'} through {@code '\u005CuFF19'},
1706 * Many other character ranges contain digits as well.
1708 * <p><b>Note:</b> This method cannot handle <a
1709 * href="#supplementary"> supplementary characters</a>. To support
1710 * all Unicode characters, including supplementary characters, use
1711 * the {@link #isDigit(int)} method.
1713 * @param ch the character to be tested.
1714 * @return {@code true} if the character is a digit;
1715 * {@code false} otherwise.
1716 * @see Character#digit(char, int)
1717 * @see Character#forDigit(int, int)
1718 * @see Character#getType(char)
1720 public static boolean isDigit(char ch) {
1721 return String.valueOf(ch).matches("\\d");
1725 * Determines if the specified character (Unicode code point) is a digit.
1727 * A character is a digit if its general category type, provided
1728 * by {@link Character#getType(int) getType(codePoint)}, is
1729 * {@code DECIMAL_DIGIT_NUMBER}.
1731 * Some Unicode character ranges that contain digits:
1733 * <li>{@code '\u005Cu0030'} through {@code '\u005Cu0039'},
1734 * ISO-LATIN-1 digits ({@code '0'} through {@code '9'})
1735 * <li>{@code '\u005Cu0660'} through {@code '\u005Cu0669'},
1736 * Arabic-Indic digits
1737 * <li>{@code '\u005Cu06F0'} through {@code '\u005Cu06F9'},
1738 * Extended Arabic-Indic digits
1739 * <li>{@code '\u005Cu0966'} through {@code '\u005Cu096F'},
1741 * <li>{@code '\u005CuFF10'} through {@code '\u005CuFF19'},
1745 * Many other character ranges contain digits as well.
1747 * @param codePoint the character (Unicode code point) to be tested.
1748 * @return {@code true} if the character is a digit;
1749 * {@code false} otherwise.
1750 * @see Character#forDigit(int, int)
1751 * @see Character#getType(int)
1754 public static boolean isDigit(int codePoint) {
1755 return fromCodeChars(codePoint).matches("\\d");
1758 @JavaScriptBody(args = "c", body = "return String.fromCharCode(c);")
1759 private native static String fromCodeChars(int codePoint);
1762 * Determines if a character is defined in Unicode.
1764 * A character is defined if at least one of the following is true:
1766 * <li>It has an entry in the UnicodeData file.
1767 * <li>It has a value in a range defined by the UnicodeData file.
1770 * <p><b>Note:</b> This method cannot handle <a
1771 * href="#supplementary"> supplementary characters</a>. To support
1772 * all Unicode characters, including supplementary characters, use
1773 * the {@link #isDefined(int)} method.
1775 * @param ch the character to be tested
1776 * @return {@code true} if the character has a defined meaning
1777 * in Unicode; {@code false} otherwise.
1778 * @see Character#isDigit(char)
1779 * @see Character#isLetter(char)
1780 * @see Character#isLetterOrDigit(char)
1781 * @see Character#isLowerCase(char)
1782 * @see Character#isTitleCase(char)
1783 * @see Character#isUpperCase(char)
1786 public static boolean isDefined(char ch) {
1787 return isDefined((int)ch);
1791 * Determines if a character (Unicode code point) is defined in Unicode.
1793 * A character is defined if at least one of the following is true:
1795 * <li>It has an entry in the UnicodeData file.
1796 * <li>It has a value in a range defined by the UnicodeData file.
1799 * @param codePoint the character (Unicode code point) to be tested.
1800 * @return {@code true} if the character has a defined meaning
1801 * in Unicode; {@code false} otherwise.
1802 * @see Character#isDigit(int)
1803 * @see Character#isLetter(int)
1804 * @see Character#isLetterOrDigit(int)
1805 * @see Character#isLowerCase(int)
1806 * @see Character#isTitleCase(int)
1807 * @see Character#isUpperCase(int)
1810 public static boolean isDefined(int codePoint) {
1811 return getType(codePoint) != Character.UNASSIGNED;
1815 * Determines if the specified character is a letter.
1817 * A character is considered to be a letter if its general
1818 * category type, provided by {@code Character.getType(ch)},
1819 * is any of the following:
1821 * <li> {@code UPPERCASE_LETTER}
1822 * <li> {@code LOWERCASE_LETTER}
1823 * <li> {@code TITLECASE_LETTER}
1824 * <li> {@code MODIFIER_LETTER}
1825 * <li> {@code OTHER_LETTER}
1828 * Not all letters have case. Many characters are
1829 * letters but are neither uppercase nor lowercase nor titlecase.
1831 * <p><b>Note:</b> This method cannot handle <a
1832 * href="#supplementary"> supplementary characters</a>. To support
1833 * all Unicode characters, including supplementary characters, use
1834 * the {@link #isLetter(int)} method.
1836 * @param ch the character to be tested.
1837 * @return {@code true} if the character is a letter;
1838 * {@code false} otherwise.
1839 * @see Character#isDigit(char)
1840 * @see Character#isJavaIdentifierStart(char)
1841 * @see Character#isJavaLetter(char)
1842 * @see Character#isJavaLetterOrDigit(char)
1843 * @see Character#isLetterOrDigit(char)
1844 * @see Character#isLowerCase(char)
1845 * @see Character#isTitleCase(char)
1846 * @see Character#isUnicodeIdentifierStart(char)
1847 * @see Character#isUpperCase(char)
1849 public static boolean isLetter(char ch) {
1850 return String.valueOf(ch).matches("\\w") && !isDigit(ch);
1854 * Determines if the specified character (Unicode code point) is a letter.
1856 * A character is considered to be a letter if its general
1857 * category type, provided by {@link Character#getType(int) getType(codePoint)},
1858 * is any of the following:
1860 * <li> {@code UPPERCASE_LETTER}
1861 * <li> {@code LOWERCASE_LETTER}
1862 * <li> {@code TITLECASE_LETTER}
1863 * <li> {@code MODIFIER_LETTER}
1864 * <li> {@code OTHER_LETTER}
1867 * Not all letters have case. Many characters are
1868 * letters but are neither uppercase nor lowercase nor titlecase.
1870 * @param codePoint the character (Unicode code point) to be tested.
1871 * @return {@code true} if the character is a letter;
1872 * {@code false} otherwise.
1873 * @see Character#isDigit(int)
1874 * @see Character#isJavaIdentifierStart(int)
1875 * @see Character#isLetterOrDigit(int)
1876 * @see Character#isLowerCase(int)
1877 * @see Character#isTitleCase(int)
1878 * @see Character#isUnicodeIdentifierStart(int)
1879 * @see Character#isUpperCase(int)
1882 public static boolean isLetter(int codePoint) {
1883 return fromCodeChars(codePoint).matches("\\w") && !isDigit(codePoint);
1887 * Determines if the specified character is a letter or digit.
1889 * A character is considered to be a letter or digit if either
1890 * {@code Character.isLetter(char ch)} or
1891 * {@code Character.isDigit(char ch)} returns
1892 * {@code true} for the character.
1894 * <p><b>Note:</b> This method cannot handle <a
1895 * href="#supplementary"> supplementary characters</a>. To support
1896 * all Unicode characters, including supplementary characters, use
1897 * the {@link #isLetterOrDigit(int)} method.
1899 * @param ch the character to be tested.
1900 * @return {@code true} if the character is a letter or digit;
1901 * {@code false} otherwise.
1902 * @see Character#isDigit(char)
1903 * @see Character#isJavaIdentifierPart(char)
1904 * @see Character#isJavaLetter(char)
1905 * @see Character#isJavaLetterOrDigit(char)
1906 * @see Character#isLetter(char)
1907 * @see Character#isUnicodeIdentifierPart(char)
1910 public static boolean isLetterOrDigit(char ch) {
1911 return String.valueOf(ch).matches("\\w");
1915 * Determines if the specified character (Unicode code point) is a letter or digit.
1917 * A character is considered to be a letter or digit if either
1918 * {@link #isLetter(int) isLetter(codePoint)} or
1919 * {@link #isDigit(int) isDigit(codePoint)} returns
1920 * {@code true} for the character.
1922 * @param codePoint the character (Unicode code point) to be tested.
1923 * @return {@code true} if the character is a letter or digit;
1924 * {@code false} otherwise.
1925 * @see Character#isDigit(int)
1926 * @see Character#isJavaIdentifierPart(int)
1927 * @see Character#isLetter(int)
1928 * @see Character#isUnicodeIdentifierPart(int)
1931 public static boolean isLetterOrDigit(int codePoint) {
1932 return fromCodeChars(codePoint).matches("\\w");
1935 public static int getType(int x) {
1936 throw new UnsupportedOperationException("getType: " + (char)x);
1940 * Determines if the specified character is
1941 * permissible as the first character in a Java identifier.
1943 * A character may start a Java identifier if and only if
1944 * one of the following conditions is true:
1946 * <li> {@link #isLetter(char) isLetter(ch)} returns {@code true}
1947 * <li> {@link #getType(char) getType(ch)} returns {@code LETTER_NUMBER}
1948 * <li> {@code ch} is a currency symbol (such as {@code '$'})
1949 * <li> {@code ch} is a connecting punctuation character (such as {@code '_'}).
1952 * <p><b>Note:</b> This method cannot handle <a
1953 * href="#supplementary"> supplementary characters</a>. To support
1954 * all Unicode characters, including supplementary characters, use
1955 * the {@link #isJavaIdentifierStart(int)} method.
1957 * @param ch the character to be tested.
1958 * @return {@code true} if the character may start a Java identifier;
1959 * {@code false} otherwise.
1960 * @see Character#isJavaIdentifierPart(char)
1961 * @see Character#isLetter(char)
1962 * @see Character#isUnicodeIdentifierStart(char)
1963 * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
1966 public static boolean isJavaIdentifierStart(char ch) {
1967 return isJavaIdentifierStart((int)ch);
1971 * Determines if the character (Unicode code point) is
1972 * permissible as the first character in a Java identifier.
1974 * A character may start a Java identifier if and only if
1975 * one of the following conditions is true:
1977 * <li> {@link #isLetter(int) isLetter(codePoint)}
1978 * returns {@code true}
1979 * <li> {@link #getType(int) getType(codePoint)}
1980 * returns {@code LETTER_NUMBER}
1981 * <li> the referenced character is a currency symbol (such as {@code '$'})
1982 * <li> the referenced character is a connecting punctuation character
1983 * (such as {@code '_'}).
1986 * @param codePoint the character (Unicode code point) to be tested.
1987 * @return {@code true} if the character may start a Java identifier;
1988 * {@code false} otherwise.
1989 * @see Character#isJavaIdentifierPart(int)
1990 * @see Character#isLetter(int)
1991 * @see Character#isUnicodeIdentifierStart(int)
1992 * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
1995 public static boolean isJavaIdentifierStart(int codePoint) {
1997 ('A' <= codePoint && codePoint <= 'Z') ||
1998 ('a' <= codePoint && codePoint <= 'z');
2002 * Determines if the specified character may be part of a Java
2003 * identifier as other than the first character.
2005 * A character may be part of a Java identifier if any of the following
2008 * <li> it is a letter
2009 * <li> it is a currency symbol (such as {@code '$'})
2010 * <li> it is a connecting punctuation character (such as {@code '_'})
2011 * <li> it is a digit
2012 * <li> it is a numeric letter (such as a Roman numeral character)
2013 * <li> it is a combining mark
2014 * <li> it is a non-spacing mark
2015 * <li> {@code isIdentifierIgnorable} returns
2016 * {@code true} for the character
2019 * <p><b>Note:</b> This method cannot handle <a
2020 * href="#supplementary"> supplementary characters</a>. To support
2021 * all Unicode characters, including supplementary characters, use
2022 * the {@link #isJavaIdentifierPart(int)} method.
2024 * @param ch the character to be tested.
2025 * @return {@code true} if the character may be part of a
2026 * Java identifier; {@code false} otherwise.
2027 * @see Character#isIdentifierIgnorable(char)
2028 * @see Character#isJavaIdentifierStart(char)
2029 * @see Character#isLetterOrDigit(char)
2030 * @see Character#isUnicodeIdentifierPart(char)
2031 * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
2034 public static boolean isJavaIdentifierPart(char ch) {
2035 return isJavaIdentifierPart((int)ch);
2039 * Determines if the character (Unicode code point) may be part of a Java
2040 * identifier as other than the first character.
2042 * A character may be part of a Java identifier if any of the following
2045 * <li> it is a letter
2046 * <li> it is a currency symbol (such as {@code '$'})
2047 * <li> it is a connecting punctuation character (such as {@code '_'})
2048 * <li> it is a digit
2049 * <li> it is a numeric letter (such as a Roman numeral character)
2050 * <li> it is a combining mark
2051 * <li> it is a non-spacing mark
2052 * <li> {@link #isIdentifierIgnorable(int)
2053 * isIdentifierIgnorable(codePoint)} returns {@code true} for
2057 * @param codePoint the character (Unicode code point) to be tested.
2058 * @return {@code true} if the character may be part of a
2059 * Java identifier; {@code false} otherwise.
2060 * @see Character#isIdentifierIgnorable(int)
2061 * @see Character#isJavaIdentifierStart(int)
2062 * @see Character#isLetterOrDigit(int)
2063 * @see Character#isUnicodeIdentifierPart(int)
2064 * @see javax.lang.model.SourceVersion#isIdentifier(CharSequence)
2067 public static boolean isJavaIdentifierPart(int codePoint) {
2068 return isJavaIdentifierStart(codePoint) ||
2069 ('0' <= codePoint && codePoint <= '9') || codePoint == '$';
2073 * Converts the character argument to lowercase using case
2074 * mapping information from the UnicodeData file.
2077 * {@code Character.isLowerCase(Character.toLowerCase(ch))}
2078 * does not always return {@code true} for some ranges of
2079 * characters, particularly those that are symbols or ideographs.
2081 * <p>In general, {@link String#toLowerCase()} should be used to map
2082 * characters to lowercase. {@code String} case mapping methods
2083 * have several benefits over {@code Character} case mapping methods.
2084 * {@code String} case mapping methods can perform locale-sensitive
2085 * mappings, context-sensitive mappings, and 1:M character mappings, whereas
2086 * the {@code Character} case mapping methods cannot.
2088 * <p><b>Note:</b> This method cannot handle <a
2089 * href="#supplementary"> supplementary characters</a>. To support
2090 * all Unicode characters, including supplementary characters, use
2091 * the {@link #toLowerCase(int)} method.
2093 * @param ch the character to be converted.
2094 * @return the lowercase equivalent of the character, if any;
2095 * otherwise, the character itself.
2096 * @see Character#isLowerCase(char)
2097 * @see String#toLowerCase()
2099 public static char toLowerCase(char ch) {
2100 return String.valueOf(ch).toLowerCase().charAt(0);
2104 * Converts the character argument to uppercase using case mapping
2105 * information from the UnicodeData file.
2108 * {@code Character.isUpperCase(Character.toUpperCase(ch))}
2109 * does not always return {@code true} for some ranges of
2110 * characters, particularly those that are symbols or ideographs.
2112 * <p>In general, {@link String#toUpperCase()} should be used to map
2113 * characters to uppercase. {@code String} case mapping methods
2114 * have several benefits over {@code Character} case mapping methods.
2115 * {@code String} case mapping methods can perform locale-sensitive
2116 * mappings, context-sensitive mappings, and 1:M character mappings, whereas
2117 * the {@code Character} case mapping methods cannot.
2119 * <p><b>Note:</b> This method cannot handle <a
2120 * href="#supplementary"> supplementary characters</a>. To support
2121 * all Unicode characters, including supplementary characters, use
2122 * the {@link #toUpperCase(int)} method.
2124 * @param ch the character to be converted.
2125 * @return the uppercase equivalent of the character, if any;
2126 * otherwise, the character itself.
2127 * @see Character#isUpperCase(char)
2128 * @see String#toUpperCase()
2130 public static char toUpperCase(char ch) {
2131 return String.valueOf(ch).toUpperCase().charAt(0);
2135 * Returns the numeric value of the character {@code ch} in the
2138 * If the radix is not in the range {@code MIN_RADIX} ≤
2139 * {@code radix} ≤ {@code MAX_RADIX} or if the
2140 * value of {@code ch} is not a valid digit in the specified
2141 * radix, {@code -1} is returned. A character is a valid digit
2142 * if at least one of the following is true:
2144 * <li>The method {@code isDigit} is {@code true} of the character
2145 * and the Unicode decimal digit value of the character (or its
2146 * single-character decomposition) is less than the specified radix.
2147 * In this case the decimal digit value is returned.
2148 * <li>The character is one of the uppercase Latin letters
2149 * {@code 'A'} through {@code 'Z'} and its code is less than
2150 * {@code radix + 'A' - 10}.
2151 * In this case, {@code ch - 'A' + 10}
2153 * <li>The character is one of the lowercase Latin letters
2154 * {@code 'a'} through {@code 'z'} and its code is less than
2155 * {@code radix + 'a' - 10}.
2156 * In this case, {@code ch - 'a' + 10}
2158 * <li>The character is one of the fullwidth uppercase Latin letters A
2159 * ({@code '\u005CuFF21'}) through Z ({@code '\u005CuFF3A'})
2160 * and its code is less than
2161 * {@code radix + '\u005CuFF21' - 10}.
2162 * In this case, {@code ch - '\u005CuFF21' + 10}
2164 * <li>The character is one of the fullwidth lowercase Latin letters a
2165 * ({@code '\u005CuFF41'}) through z ({@code '\u005CuFF5A'})
2166 * and its code is less than
2167 * {@code radix + '\u005CuFF41' - 10}.
2168 * In this case, {@code ch - '\u005CuFF41' + 10}
2172 * <p><b>Note:</b> This method cannot handle <a
2173 * href="#supplementary"> supplementary characters</a>. To support
2174 * all Unicode characters, including supplementary characters, use
2175 * the {@link #digit(int, int)} method.
2177 * @param ch the character to be converted.
2178 * @param radix the radix.
2179 * @return the numeric value represented by the character in the
2181 * @see Character#forDigit(int, int)
2182 * @see Character#isDigit(char)
2184 public static int digit(char ch, int radix) {
2185 return digit((int)ch, radix);
2189 * Returns the numeric value of the specified character (Unicode
2190 * code point) in the specified radix.
2192 * <p>If the radix is not in the range {@code MIN_RADIX} ≤
2193 * {@code radix} ≤ {@code MAX_RADIX} or if the
2194 * character is not a valid digit in the specified
2195 * radix, {@code -1} is returned. A character is a valid digit
2196 * if at least one of the following is true:
2198 * <li>The method {@link #isDigit(int) isDigit(codePoint)} is {@code true} of the character
2199 * and the Unicode decimal digit value of the character (or its
2200 * single-character decomposition) is less than the specified radix.
2201 * In this case the decimal digit value is returned.
2202 * <li>The character is one of the uppercase Latin letters
2203 * {@code 'A'} through {@code 'Z'} and its code is less than
2204 * {@code radix + 'A' - 10}.
2205 * In this case, {@code codePoint - 'A' + 10}
2207 * <li>The character is one of the lowercase Latin letters
2208 * {@code 'a'} through {@code 'z'} and its code is less than
2209 * {@code radix + 'a' - 10}.
2210 * In this case, {@code codePoint - 'a' + 10}
2212 * <li>The character is one of the fullwidth uppercase Latin letters A
2213 * ({@code '\u005CuFF21'}) through Z ({@code '\u005CuFF3A'})
2214 * and its code is less than
2215 * {@code radix + '\u005CuFF21' - 10}.
2217 * {@code codePoint - '\u005CuFF21' + 10}
2219 * <li>The character is one of the fullwidth lowercase Latin letters a
2220 * ({@code '\u005CuFF41'}) through z ({@code '\u005CuFF5A'})
2221 * and its code is less than
2222 * {@code radix + '\u005CuFF41'- 10}.
2224 * {@code codePoint - '\u005CuFF41' + 10}
2228 * @param codePoint the character (Unicode code point) to be converted.
2229 * @param radix the radix.
2230 * @return the numeric value represented by the character in the
2232 * @see Character#forDigit(int, int)
2233 * @see Character#isDigit(int)
2236 @JavaScriptBody(args = { "codePoint", "radix" }, body=
2237 "var x = parseInt(String.fromCharCode(codePoint), radix);\n"
2238 + "return isNaN(x) ? -1 : x;"
2240 public static int digit(int codePoint, int radix) {
2241 throw new UnsupportedOperationException();
2245 * Returns the {@code int} value that the specified Unicode
2246 * character represents. For example, the character
2247 * {@code '\u005Cu216C'} (the roman numeral fifty) will return
2248 * an int with a value of 50.
2250 * The letters A-Z in their uppercase ({@code '\u005Cu0041'} through
2251 * {@code '\u005Cu005A'}), lowercase
2252 * ({@code '\u005Cu0061'} through {@code '\u005Cu007A'}), and
2253 * full width variant ({@code '\u005CuFF21'} through
2254 * {@code '\u005CuFF3A'} and {@code '\u005CuFF41'} through
2255 * {@code '\u005CuFF5A'}) forms have numeric values from 10
2256 * through 35. This is independent of the Unicode specification,
2257 * which does not assign numeric values to these {@code char}
2260 * If the character does not have a numeric value, then -1 is returned.
2261 * If the character has a numeric value that cannot be represented as a
2262 * nonnegative integer (for example, a fractional value), then -2
2265 * <p><b>Note:</b> This method cannot handle <a
2266 * href="#supplementary"> supplementary characters</a>. To support
2267 * all Unicode characters, including supplementary characters, use
2268 * the {@link #getNumericValue(int)} method.
2270 * @param ch the character to be converted.
2271 * @return the numeric value of the character, as a nonnegative {@code int}
2272 * value; -2 if the character has a numeric value that is not a
2273 * nonnegative integer; -1 if the character has no numeric value.
2274 * @see Character#forDigit(int, int)
2275 * @see Character#isDigit(char)
2278 public static int getNumericValue(char ch) {
2279 return getNumericValue((int)ch);
2283 * Returns the {@code int} value that the specified
2284 * character (Unicode code point) represents. For example, the character
2285 * {@code '\u005Cu216C'} (the Roman numeral fifty) will return
2286 * an {@code int} with a value of 50.
2288 * The letters A-Z in their uppercase ({@code '\u005Cu0041'} through
2289 * {@code '\u005Cu005A'}), lowercase
2290 * ({@code '\u005Cu0061'} through {@code '\u005Cu007A'}), and
2291 * full width variant ({@code '\u005CuFF21'} through
2292 * {@code '\u005CuFF3A'} and {@code '\u005CuFF41'} through
2293 * {@code '\u005CuFF5A'}) forms have numeric values from 10
2294 * through 35. This is independent of the Unicode specification,
2295 * which does not assign numeric values to these {@code char}
2298 * If the character does not have a numeric value, then -1 is returned.
2299 * If the character has a numeric value that cannot be represented as a
2300 * nonnegative integer (for example, a fractional value), then -2
2303 * @param codePoint the character (Unicode code point) to be converted.
2304 * @return the numeric value of the character, as a nonnegative {@code int}
2305 * value; -2 if the character has a numeric value that is not a
2306 * nonnegative integer; -1 if the character has no numeric value.
2307 * @see Character#forDigit(int, int)
2308 * @see Character#isDigit(int)
2311 public static int getNumericValue(int codePoint) {
2312 throw new UnsupportedOperationException();
2316 * Determines if the specified character is ISO-LATIN-1 white space.
2317 * This method returns {@code true} for the following five
2320 * <tr><td>{@code '\t'}</td> <td>{@code U+0009}</td>
2321 * <td>{@code HORIZONTAL TABULATION}</td></tr>
2322 * <tr><td>{@code '\n'}</td> <td>{@code U+000A}</td>
2323 * <td>{@code NEW LINE}</td></tr>
2324 * <tr><td>{@code '\f'}</td> <td>{@code U+000C}</td>
2325 * <td>{@code FORM FEED}</td></tr>
2326 * <tr><td>{@code '\r'}</td> <td>{@code U+000D}</td>
2327 * <td>{@code CARRIAGE RETURN}</td></tr>
2328 * <tr><td>{@code ' '}</td> <td>{@code U+0020}</td>
2329 * <td>{@code SPACE}</td></tr>
2332 * @param ch the character to be tested.
2333 * @return {@code true} if the character is ISO-LATIN-1 white
2334 * space; {@code false} otherwise.
2335 * @see Character#isSpaceChar(char)
2336 * @see Character#isWhitespace(char)
2337 * @deprecated Replaced by isWhitespace(char).
2340 public static boolean isSpace(char ch) {
2341 return isSpaceChar(ch);
2344 public static boolean isSpaceChar(int ch) {
2345 return (ch <= 0x0020) &&
2346 (((((1L << 0x0009) |
2350 (1L << 0x0020)) >> ch) & 1L) != 0);
2355 * Determines if the specified character is white space according to Java.
2356 * A character is a Java whitespace character if and only if it satisfies
2357 * one of the following criteria:
2359 * <li> It is a Unicode space character ({@code SPACE_SEPARATOR},
2360 * {@code LINE_SEPARATOR}, or {@code PARAGRAPH_SEPARATOR})
2361 * but is not also a non-breaking space ({@code '\u005Cu00A0'},
2362 * {@code '\u005Cu2007'}, {@code '\u005Cu202F'}).
2363 * <li> It is {@code '\u005Ct'}, U+0009 HORIZONTAL TABULATION.
2364 * <li> It is {@code '\u005Cn'}, U+000A LINE FEED.
2365 * <li> It is {@code '\u005Cu000B'}, U+000B VERTICAL TABULATION.
2366 * <li> It is {@code '\u005Cf'}, U+000C FORM FEED.
2367 * <li> It is {@code '\u005Cr'}, U+000D CARRIAGE RETURN.
2368 * <li> It is {@code '\u005Cu001C'}, U+001C FILE SEPARATOR.
2369 * <li> It is {@code '\u005Cu001D'}, U+001D GROUP SEPARATOR.
2370 * <li> It is {@code '\u005Cu001E'}, U+001E RECORD SEPARATOR.
2371 * <li> It is {@code '\u005Cu001F'}, U+001F UNIT SEPARATOR.
2374 * <p><b>Note:</b> This method cannot handle <a
2375 * href="#supplementary"> supplementary characters</a>. To support
2376 * all Unicode characters, including supplementary characters, use
2377 * the {@link #isWhitespace(int)} method.
2379 * @param ch the character to be tested.
2380 * @return {@code true} if the character is a Java whitespace
2381 * character; {@code false} otherwise.
2382 * @see Character#isSpaceChar(char)
2385 public static boolean isWhitespace(char ch) {
2386 return isWhitespace((int)ch);
2390 * Determines if the specified character (Unicode code point) is
2391 * white space according to Java. A character is a Java
2392 * whitespace character if and only if it satisfies one of the
2393 * following criteria:
2395 * <li> It is a Unicode space character ({@link #SPACE_SEPARATOR},
2396 * {@link #LINE_SEPARATOR}, or {@link #PARAGRAPH_SEPARATOR})
2397 * but is not also a non-breaking space ({@code '\u005Cu00A0'},
2398 * {@code '\u005Cu2007'}, {@code '\u005Cu202F'}).
2399 * <li> It is {@code '\u005Ct'}, U+0009 HORIZONTAL TABULATION.
2400 * <li> It is {@code '\u005Cn'}, U+000A LINE FEED.
2401 * <li> It is {@code '\u005Cu000B'}, U+000B VERTICAL TABULATION.
2402 * <li> It is {@code '\u005Cf'}, U+000C FORM FEED.
2403 * <li> It is {@code '\u005Cr'}, U+000D CARRIAGE RETURN.
2404 * <li> It is {@code '\u005Cu001C'}, U+001C FILE SEPARATOR.
2405 * <li> It is {@code '\u005Cu001D'}, U+001D GROUP SEPARATOR.
2406 * <li> It is {@code '\u005Cu001E'}, U+001E RECORD SEPARATOR.
2407 * <li> It is {@code '\u005Cu001F'}, U+001F UNIT SEPARATOR.
2411 * @param codePoint the character (Unicode code point) to be tested.
2412 * @return {@code true} if the character is a Java whitespace
2413 * character; {@code false} otherwise.
2414 * @see Character#isSpaceChar(int)
2417 public static boolean isWhitespace(int codePoint) {
2419 codePoint == SPACE_SEPARATOR ||
2420 codePoint == LINE_SEPARATOR ||
2421 codePoint == PARAGRAPH_SEPARATOR
2429 * Determines if the specified character is an ISO control
2430 * character. A character is considered to be an ISO control
2431 * character if its code is in the range {@code '\u005Cu0000'}
2432 * through {@code '\u005Cu001F'} or in the range
2433 * {@code '\u005Cu007F'} through {@code '\u005Cu009F'}.
2435 * <p><b>Note:</b> This method cannot handle <a
2436 * href="#supplementary"> supplementary characters</a>. To support
2437 * all Unicode characters, including supplementary characters, use
2438 * the {@link #isISOControl(int)} method.
2440 * @param ch the character to be tested.
2441 * @return {@code true} if the character is an ISO control character;
2442 * {@code false} otherwise.
2444 * @see Character#isSpaceChar(char)
2445 * @see Character#isWhitespace(char)
2448 public static boolean isISOControl(char ch) {
2449 return isISOControl((int)ch);
2453 * Determines if the referenced character (Unicode code point) is an ISO control
2454 * character. A character is considered to be an ISO control
2455 * character if its code is in the range {@code '\u005Cu0000'}
2456 * through {@code '\u005Cu001F'} or in the range
2457 * {@code '\u005Cu007F'} through {@code '\u005Cu009F'}.
2459 * @param codePoint the character (Unicode code point) to be tested.
2460 * @return {@code true} if the character is an ISO control character;
2461 * {@code false} otherwise.
2462 * @see Character#isSpaceChar(int)
2463 * @see Character#isWhitespace(int)
2466 public static boolean isISOControl(int codePoint) {
2467 // Optimized form of:
2468 // (codePoint >= 0x00 && codePoint <= 0x1F) ||
2469 // (codePoint >= 0x7F && codePoint <= 0x9F);
2470 return codePoint <= 0x9F &&
2471 (codePoint >= 0x7F || (codePoint >>> 5 == 0));
2475 * Determines the character representation for a specific digit in
2476 * the specified radix. If the value of {@code radix} is not a
2477 * valid radix, or the value of {@code digit} is not a valid
2478 * digit in the specified radix, the null character
2479 * ({@code '\u005Cu0000'}) is returned.
2481 * The {@code radix} argument is valid if it is greater than or
2482 * equal to {@code MIN_RADIX} and less than or equal to
2483 * {@code MAX_RADIX}. The {@code digit} argument is valid if
2484 * {@code 0 <= digit < radix}.
2486 * If the digit is less than 10, then
2487 * {@code '0' + digit} is returned. Otherwise, the value
2488 * {@code 'a' + digit - 10} is returned.
2490 * @param digit the number to convert to a character.
2491 * @param radix the radix.
2492 * @return the {@code char} representation of the specified digit
2493 * in the specified radix.
2494 * @see Character#MIN_RADIX
2495 * @see Character#MAX_RADIX
2496 * @see Character#digit(char, int)
2498 public static char forDigit(int digit, int radix) {
2499 if ((digit >= radix) || (digit < 0)) {
2502 if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX)) {
2506 return (char)('0' + digit);
2508 return (char)('a' - 10 + digit);
2512 * Compares two {@code Character} objects numerically.
2514 * @param anotherCharacter the {@code Character} to be compared.
2516 * @return the value {@code 0} if the argument {@code Character}
2517 * is equal to this {@code Character}; a value less than
2518 * {@code 0} if this {@code Character} is numerically less
2519 * than the {@code Character} argument; and a value greater than
2520 * {@code 0} if this {@code Character} is numerically greater
2521 * than the {@code Character} argument (unsigned comparison).
2522 * Note that this is strictly a numerical comparison; it is not
2526 public int compareTo(Character anotherCharacter) {
2527 return compare(this.value, anotherCharacter.value);
2531 * Compares two {@code char} values numerically.
2532 * The value returned is identical to what would be returned by:
2534 * Character.valueOf(x).compareTo(Character.valueOf(y))
2537 * @param x the first {@code char} to compare
2538 * @param y the second {@code char} to compare
2539 * @return the value {@code 0} if {@code x == y};
2540 * a value less than {@code 0} if {@code x < y}; and
2541 * a value greater than {@code 0} if {@code x > y}
2544 public static int compare(char x, char y) {
2550 * The number of bits used to represent a <tt>char</tt> value in unsigned
2551 * binary form, constant {@code 16}.
2555 public static final int SIZE = 16;
2558 * Returns the value obtained by reversing the order of the bytes in the
2559 * specified <tt>char</tt> value.
2561 * @return the value obtained by reversing (or, equivalently, swapping)
2562 * the bytes in the specified <tt>char</tt> value.
2565 public static char reverseBytes(char ch) {
2566 return (char) (((ch & 0xFF00) >> 8) | (ch << 8));
2570 // as last step of initialization, initialize valueOf method
2573 @JavaScriptBody(args = {}, body =
2574 "vm.java_lang_Character(false)." +
2575 "valueOf = function() { return this._value(); };"
2577 private native static void initValueOf();