< prev index next >

src/java.base/share/classes/java/lang/Character.java

Print this page

    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/tr27 Unicode 3.1.0
  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}.

 8997     }
 8998 
 8999     /**
 9000      * Returns a {@code Character} instance representing the specified
 9001      * {@code char} value.
 9002      * If a new {@code Character} instance is not required, this method
 9003      * should generally be used in preference to the constructor
 9004      * {@link #Character(char)}, as this method is likely to yield
 9005      * significantly better space and time performance by caching
 9006      * frequently requested values.
 9007      *
 9008      * This method will always cache values in the range {@code
 9009      * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
 9010      * cache other values outside of this range.
 9011      *
 9012      * @param  c a char value.
 9013      * @return a {@code Character} instance representing {@code c}.
 9014      * @since  1.5
 9015      */
 9016     @IntrinsicCandidate

 9017     public static Character valueOf(char c) {
 9018         if (c <= 127) { // must cache
 9019             return CharacterCache.cache[(int)c];
 9020         }
 9021         return new Character(c);
 9022     }
 9023 
 9024     /**
 9025      * Returns the value of this {@code Character} object.
 9026      * @return  the primitive {@code char} value represented by
 9027      *          this object.
 9028      */
 9029     @IntrinsicCandidate
 9030     public char charValue() {
 9031         return value;
 9032     }
 9033 
 9034     /**
 9035      * Returns a hash code for this {@code Character}; equal to the result
 9036      * 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.value.DeserializeConstructor;
   30 import jdk.internal.vm.annotation.IntrinsicCandidate;
   31 import jdk.internal.vm.annotation.Stable;
   32 
   33 import java.lang.constant.Constable;
   34 import java.lang.constant.DynamicConstantDesc;
   35 import java.util.Arrays;
   36 import java.util.HashMap;
   37 import java.util.Locale;
   38 import java.util.Map;
   39 import java.util.Objects;
   40 import java.util.Optional;
   41 
   42 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
   43 import static java.lang.constant.ConstantDescs.CD_char;
   44 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
   45 
   46 /**
   47  * The {@code Character} class is the {@linkplain
   48  * java.lang##wrapperClass wrapper class} for values of the primitive
   49  * type {@code char}. An object of type {@code Character} contains a

  150  * surrogate ranges as undefined characters. For example,
  151  * {@code Character.isLetter('\u005CuD840')} returns {@code false}, even though
  152  * this specific value if followed by any low-surrogate value in a string
  153  * would represent a letter.
  154  *
  155  * <li>The methods that accept an {@code int} value support all
  156  * Unicode characters, including supplementary characters. For
  157  * example, {@code Character.isLetter(0x2F81A)} returns
  158  * {@code true} because the code point value represents a letter
  159  * (a CJK ideograph).
  160  * </ul>
  161  *
  162  * <p>In the Java SE API documentation, <em>Unicode code point</em> is
  163  * used for character values in the range between U+0000 and U+10FFFF,
  164  * and <em>Unicode code unit</em> is used for 16-bit
  165  * {@code char} values that are code units of the <em>UTF-16</em>
  166  * encoding. For more information on Unicode terminology, refer to the
  167  * <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
  168  *
  169  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  170  * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
  171  * as interchangeable and should not use instances for synchronization, mutexes, or
  172  * with {@linkplain java.lang.ref.Reference object references}.
  173  *
  174  * <div class="preview-block">
  175  *      <div class="preview-comment">
  176  *          When preview features are enabled, {@code Character} is a {@linkplain Class#isValue value class}.
  177  *          Use of value class instances for synchronization, mutexes, or with
  178  *          {@linkplain java.lang.ref.Reference object references} result in
  179  *          {@link IdentityException}.
  180  *      </div>
  181  * </div>
  182  *
  183  * @spec https://www.unicode.org/reports/tr27 Unicode 3.1.0
  184  * @author  Lee Boynton
  185  * @author  Guy Steele
  186  * @author  Akira Tanaka
  187  * @author  Martin Buchholz
  188  * @author  Ulf Zibis
  189  * @since   1.0
  190  */
  191 @jdk.internal.MigratedValueClass
  192 @jdk.internal.ValueBased
  193 public final class Character implements java.io.Serializable, Comparable<Character>, Constable {

  194     /**
  195      * The minimum radix available for conversion to and from strings.
  196      * The constant value of this field is the smallest value permitted
  197      * for the radix argument in radix-conversion methods such as the
  198      * {@code digit} method, the {@code forDigit} method, and the
  199      * {@code toString} method of class {@code Integer}.
  200      *
  201      * @see     Character#digit(char, int)
  202      * @see     Character#forDigit(int, int)
  203      * @see     Integer#toString(int, int)
  204      * @see     Integer#valueOf(String)
  205      */
  206     public static final int MIN_RADIX = 2;
  207 
  208     /**
  209      * The maximum radix available for conversion to and from strings.
  210      * The constant value of this field is the largest value permitted
  211      * for the radix argument in radix-conversion methods such as the
  212      * {@code digit} method, the {@code forDigit} method, and the
  213      * {@code toString} method of class {@code Integer}.

 9006     }
 9007 
 9008     /**
 9009      * Returns a {@code Character} instance representing the specified
 9010      * {@code char} value.
 9011      * If a new {@code Character} instance is not required, this method
 9012      * should generally be used in preference to the constructor
 9013      * {@link #Character(char)}, as this method is likely to yield
 9014      * significantly better space and time performance by caching
 9015      * frequently requested values.
 9016      *
 9017      * This method will always cache values in the range {@code
 9018      * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
 9019      * cache other values outside of this range.
 9020      *
 9021      * @param  c a char value.
 9022      * @return a {@code Character} instance representing {@code c}.
 9023      * @since  1.5
 9024      */
 9025     @IntrinsicCandidate
 9026     @DeserializeConstructor
 9027     public static Character valueOf(char c) {
 9028         if (c <= 127) { // must cache
 9029             return CharacterCache.cache[(int)c];
 9030         }
 9031         return new Character(c);
 9032     }
 9033 
 9034     /**
 9035      * Returns the value of this {@code Character} object.
 9036      * @return  the primitive {@code char} value represented by
 9037      *          this object.
 9038      */
 9039     @IntrinsicCandidate
 9040     public char charValue() {
 9041         return value;
 9042     }
 9043 
 9044     /**
 9045      * Returns a hash code for this {@code Character}; equal to the result
 9046      * of invoking {@code charValue()}.
< prev index next >