< 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

  151  * surrogate ranges as undefined characters. For example,
  152  * {@code Character.isLetter('\u005CuD840')} returns {@code false}, even though
  153  * this specific value if followed by any low-surrogate value in a string
  154  * would represent a letter.
  155  *
  156  * <li>The methods that accept an {@code int} value support all
  157  * Unicode characters, including supplementary characters. For
  158  * example, {@code Character.isLetter(0x2F81A)} returns
  159  * {@code true} because the code point value represents a letter
  160  * (a CJK ideograph).
  161  * </ul>
  162  *
  163  * <p>In the Java SE API documentation, <em>Unicode code point</em> is
  164  * used for character values in the range between U+0000 and U+10FFFF,
  165  * and <em>Unicode code unit</em> is used for 16-bit
  166  * {@code char} values that are code units of the <em>UTF-16</em>
  167  * encoding. For more information on Unicode terminology, refer to the
  168  * <a href="http://www.unicode.org/glossary/">Unicode Glossary</a>.
  169  *
  170  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  171  * class; programmers should treat instances that are
  172  * {@linkplain #equals(Object) equal} as interchangeable and should not
  173  * use instances for synchronization, or unpredictable behavior may
  174  * occur. For example, in a future release, synchronization may fail.








  175  *
  176  * @spec https://www.unicode.org/reports/tr27 Unicode 3.1.0
  177  * @author  Lee Boynton
  178  * @author  Guy Steele
  179  * @author  Akira Tanaka
  180  * @author  Martin Buchholz
  181  * @author  Ulf Zibis
  182  * @since   1.0
  183  */

  184 @jdk.internal.ValueBased
  185 public final
  186 class Character implements java.io.Serializable, Comparable<Character>, Constable {
  187     /**
  188      * The minimum radix available for conversion to and from strings.
  189      * The constant value of this field is the smallest value permitted
  190      * for the radix argument in radix-conversion methods such as the
  191      * {@code digit} method, the {@code forDigit} method, and the
  192      * {@code toString} method of class {@code Integer}.
  193      *
  194      * @see     Character#digit(char, int)
  195      * @see     Character#forDigit(int, int)
  196      * @see     Integer#toString(int, int)
  197      * @see     Integer#valueOf(String)
  198      */
  199     public static final int MIN_RADIX = 2;
  200 
  201     /**
  202      * The maximum radix available for conversion to and from strings.
  203      * The constant value of this field is the largest value permitted
  204      * for the radix argument in radix-conversion methods such as the
  205      * {@code digit} method, the {@code forDigit} method, and the
  206      * {@code toString} method of class {@code Integer}.

 9262     }
 9263 
 9264     /**
 9265      * Returns a {@code Character} instance representing the specified
 9266      * {@code char} value.
 9267      * If a new {@code Character} instance is not required, this method
 9268      * should generally be used in preference to the constructor
 9269      * {@link #Character(char)}, as this method is likely to yield
 9270      * significantly better space and time performance by caching
 9271      * frequently requested values.
 9272      *
 9273      * This method will always cache values in the range {@code
 9274      * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
 9275      * cache other values outside of this range.
 9276      *
 9277      * @param  c a char value.
 9278      * @return a {@code Character} instance representing {@code c}.
 9279      * @since  1.5
 9280      */
 9281     @IntrinsicCandidate

 9282     public static Character valueOf(char c) {
 9283         if (c <= 127) { // must cache
 9284             return CharacterCache.cache[(int)c];
 9285         }
 9286         return new Character(c);
 9287     }
 9288 
 9289     /**
 9290      * Returns the value of this {@code Character} object.
 9291      * @return  the primitive {@code char} value represented by
 9292      *          this object.
 9293      */
 9294     @IntrinsicCandidate
 9295     public char charValue() {
 9296         return value;
 9297     }
 9298 
 9299     /**
 9300      * Returns a hash code for this {@code Character}; equal to the result
 9301      * 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

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

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

 9271     }
 9272 
 9273     /**
 9274      * Returns a {@code Character} instance representing the specified
 9275      * {@code char} value.
 9276      * If a new {@code Character} instance is not required, this method
 9277      * should generally be used in preference to the constructor
 9278      * {@link #Character(char)}, as this method is likely to yield
 9279      * significantly better space and time performance by caching
 9280      * frequently requested values.
 9281      *
 9282      * This method will always cache values in the range {@code
 9283      * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may
 9284      * cache other values outside of this range.
 9285      *
 9286      * @param  c a char value.
 9287      * @return a {@code Character} instance representing {@code c}.
 9288      * @since  1.5
 9289      */
 9290     @IntrinsicCandidate
 9291     @DeserializeConstructor
 9292     public static Character valueOf(char c) {
 9293         if (c <= 127) { // must cache
 9294             return CharacterCache.cache[(int)c];
 9295         }
 9296         return new Character(c);
 9297     }
 9298 
 9299     /**
 9300      * Returns the value of this {@code Character} object.
 9301      * @return  the primitive {@code char} value represented by
 9302      *          this object.
 9303      */
 9304     @IntrinsicCandidate
 9305     public char charValue() {
 9306         return value;
 9307     }
 9308 
 9309     /**
 9310      * Returns a hash code for this {@code Character}; equal to the result
 9311      * of invoking {@code charValue()}.
< prev index next >