< 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

  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

  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
  171  * {@linkplain #equals(Object) equal} as interchangeable and should not
  172  * use instances for synchronization, or unpredictable behavior may
  173  * occur. For example, in a future release, synchronization may fail.
  174  *
  175  * @spec https://www.unicode.org/reports/tr27 Unicode 3.1.0
  176  * @author  Lee Boynton
  177  * @author  Guy Steele
  178  * @author  Akira Tanaka
  179  * @author  Martin Buchholz
  180  * @author  Ulf Zibis
  181  * @since   1.0
  182  */
  183 @jdk.internal.MigratedValueClass
  184 @jdk.internal.ValueBased
  185 public final class Character implements java.io.Serializable, Comparable<Character>, Constable {

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

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