Documentation

The Java™ Tutorials
Hide TOC
Characters字符
Trail: Learning the Java Language
Lesson: Numbers and Strings

Characters字符

Most of the time, if you are using a single character value, you will use the primitive char type.大多数情况下,如果使用的是单个字符值,则将使用基本char类型。For example:例如:

char ch = 'a'; 
// Unicode for uppercase Greek omega character
char uniChar = '\u03A9';
// an array of chars
char[] charArray = { 'a', 'b', 'c', 'd', 'e' };

There are times, however, when you need to use a char as an object但是,有时您需要使用char作为对象for example, as a method argument where an object is expected.例如,作为预期对象的方法参数。The Java programming language provides a wrapper class that "wraps" the char in a Character object for this purpose.Java编程语言提供了一个包装器类,用于为此目的将char“包装”到Character对象中。An object of type Character contains a single field, whose type is char.Character类型的对象包含一个字段,其类型为charThis Character class also offers a number of useful class (that is, static) methods for manipulating characters.这个Character类还提供了许多有用的类(即静态)方法来操作字符。

You can create a Character object with the Character constructor:可以使用Character构造函数创建Character对象:

Character ch = new Character('a');

The Java compiler will also create a Character object for you under some circumstances.在某些情况下,Java编译器还会为您创建一个Character对象。For example, if you pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character for you.例如,如果将一个基本char传递给一个需要对象的方法,编译器会自动将该char转换为一个CharacterThis feature is called autoboxing—or unboxing, if the conversion goes the other way.此功能称为自动装箱—如果转换方向相反,则为拆箱For more information on autoboxing and unboxing, see Autoboxing and Unboxing.有关自动装箱和取消装箱的详细信息,请参阅自动装箱和拆箱


Note: The Character class is immutable, so that once it is created, a Character object cannot be changed.Character类是不可变的,因此一旦创建它,就不能更改Character对象。

The following table lists some of the most useful methods in the Character class, but is not exhaustive.下表列出了Character类中一些最有用的方法,但并非详尽无遗。For a complete listing of all methods in this class (there are more than 50), refer to the java.lang.Character API specification.有关此类中所有方法的完整列表(超过50个),请参阅java.lang.Character API规范。

Useful Methods in the Character ClassCharacter类中的有用方法
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.确定指定的字符值是否为空白。
boolean isUpperCase(char ch)
boolean isLowerCase(char ch)
Determines whether the specified char value is uppercase or lowercase, respectively.确定指定的字符值分别是大写还是小写。
char toUpperCase(char ch)
char toLowerCase(char ch)
Returns the uppercase or lowercase form of the specified char value.返回指定字符值的大小写形式。
toString(char ch) Returns a String object representing the specified character value — that is, a one-character string.返回表示指定字符值的String对象—即,一个单字符字符串。

Escape Sequences转义序列

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler.前面加反斜杠(\)的字符是转义序列,对编译器具有特殊意义。The following table shows the Java escape sequences:下表显示了Java转义序列:

Escape Sequences转义序列
Escape Sequence转义序列 Description描述
\t Insert a tab in the text at this point.此时在文本中插入一个制表符。
\b Insert a backspace in the text at this point.此时在文本中插入一个退格。
\n Insert a newline in the text at this point.此时在文本中插入换行符。
\r Insert a carriage return in the text at this point.此时在文本中插入回车符。
\f Insert a form feed in the text at this point.此时在文本中插入一个换页符。
\' Insert a single quote character in the text at this point.此时在文本中插入单引号字符。
\" Insert a double quote character in the text at this point.此时在文本中插入双引号字符。
\\ Insert a backslash character in the text at this point.此时在文本中插入反斜杠字符。

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly.当在print语句中遇到转义序列时,编译器将相应地对其进行解释。For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes.例如,如果要在引号内加引号,则必须在内部引号上使用转义序列\"To print the sentence要打印句子

She said "Hello!" to me.

you would write你应该写成

System.out.println("She said \"Hello!\" to me.");

Previous page: Questions and Exercises: Numbers
Next page: Strings