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
类型。There are times, however, when you need to use a char as an objectfor 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
类还提供了许多有用的类(即静态)方法来操作字符。
Strings are a sequence of characters and are widely used in Java programming.字符串是一个字符序列,在Java编程中被广泛使用。In the Java programming language, strings are objects.在Java编程语言中,字符串是对象。The String
class has over 60 methods and 13 constructors.String
类有60多种方法和13种构造函数。
Most commonly, you create a string with a statement like最常见的情况是,使用以下语句创建字符串
String s = "Hello world!";
rather than using one of the 而不是使用一个String
constructors.String
构造函数。
The String
class has many methods to find and retrieve substrings; these can then be easily reassembled into new strings using the +
concatenation operator.String
类有许多方法来查找和检索子字符串;然后可以使用+
串联运算符轻松地将这些字符串重新组合成新字符串。
The String
class also includes a number of utility methods, among them split()
, toLowerCase()
, toUpperCase()
, and valueOf()
.String
类还包括许多实用程序方法,其中包括split()
、toLowerCase()
、toUpperCase()
和valueOf()
。The latter method is indispensable in converting user input strings to numbers.在将用户输入字符串转换为数字时,后一种方法是必不可少的。The Number
subclasses also have methods for converting strings to numbers and vice versa.Number
子类也有将字符串转换为数字的方法,反之亦然。
In addition to the 除了String
class, there is also a StringBuilder
class.String
类之外,还有一个StringBuilder
类。Working with 使用StringBuilder
objects can sometimes be more efficient than working with strings.StringBuilder
对象有时比使用字符串更有效。The StringBuilder
class offers a few methods that can be useful for strings, among them reverse()
.StringBuilder
类提供了一些对字符串有用的方法,其中包括reverse()
。In general, however, the 但是,一般来说,String
class has a wider variety of methods.String
类有更广泛的方法。
A string can be converted to a string builder using a 可以使用StringBuilder
constructor.StringBuilder
构造函数将字符串转换为字符串生成器。A string builder can be converted to a string with the 可以使用toString()
method.toString()
方法将字符串生成器转换为字符串。