站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Java Tutorial 5.0 英文版

Characters - Java Tutorial 5.0 英文版

The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Characters

Most of the time, if you are using a single character value, you will use the primitive char type, for example:
char ch = 'a';
char[] firstFiveLetters={ 'a', 'b', 'c', 'd', 'e' };
char ch = string.charAt(i);
The Character (in a .java source file) class isn't required for such simple usage, but does offer several useful static methods. Here are some examples:
//Test whether a character is upper case.
if (Character.isUpperCase(string.charAt(i)) { ... }

//Convert a character to lower case.
char ch = Character.toLowerCase(string.charAt(i));

//Determine if the character is a letter.
if (Character.isLetter(string.charAt(i))) { ... }
The class is immutable, so once created, a Character cannot be changed.

If you are passing a primitive data type (such as a char) into a method that expects a wrapper object (such as a Character) the Java compiler automatically converts the type for you. This feature is called autoboxing or unboxing if the conversion goes the other way. Autoboxing was added to the compiler in release 5.0, so if you are using an earlier version, you'll have to do the conversion manually.

The following tables lists the most useful methods in the Character class, but is not exhaustive. For a complete listing of all methods in this class, refer to the java.lang.Character (in a .java source file) API specification.

Useful Methods in the Character Class
Method Description
boolean isLetter(char ch)
boolean isDigit(char ch)
Determines whether the specified char value is a letter or a digit, respectively.
boolean isWhiteSpace(char ch) Determines whether the specified char value is white space according to the Java platform.
boolean isUpperCase(char ch)
boolean isLowerCase(char ch)
Determines whether the specified char value is upper- or lowercase, respectively.
char toUpperCase(char ch)
char toLowerCase(char ch)
Returns the upper- or lowercase form of the specified char value.
toString(char ch) Returns a String object representing the specified character value.
int digit(char ch, int radix) Returns the numeric value of the character in the specified radix.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.