The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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 Java编程语言提供了一个包装器类,用于为此目的将char
in a Character
object for this purpose.char
“包装”到Character
对象中。An object of type Character
contains a single field, whose type is char
.Character
类型的对象包含一个字段,其类型为char
。This 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 在某些情况下,Java编译器还会为您创建一个Character
object for you under some circumstances.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
转换为一个Character
。This feature is called autoboxingor unboxing, if the conversion goes the other way.此功能称为自动装箱如果转换方向相反,则为拆箱。For more information on autoboxing and unboxing, see Autoboxing and Unboxing.有关自动装箱和取消装箱的详细信息,请参阅自动装箱和拆箱。
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规范。
boolean isLetter(char ch) |
|
boolean isWhitespace(char ch) |
|
boolean isUpperCase(char ch) |
|
char toUpperCase(char ch) |
|
toString(char ch) |
String object representing the specified character value that is, a one-character string.String 对象即,一个单字符字符串。 |
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转义序列:
\t |
|
\b |
|
\n |
|
\r |
|
\f |
|
\' |
|
\" |
|
\\ |
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.");