The Character class is used to manipulate a single character, it wraps the char value in an object.
In fact, in Java, char is a primitive data type, and Character is a wrapper class for char. Through the Character class, you can use a series of methods to manipulate characters. When creating a Character object, you can use the constructor of the Character class to pass in a char value to initialize the object, such as:
Character ch = new Character('a')。
Also, in some cases, the Java compiler does boxing (converting char to Character) and unboxing (converting Character to char) automatically.
For example, when passing a parameter of type char to a method that requires a parameter of type Character, the compiler will automatically convert the parameter of type char into a Character object. This feature is called boxing, which in turn is called unboxing.
// 原始字符 'a' 装箱到 Character 对象 ch 中
Character ch = 'a';
// 原始字符 'x' 用 test 方法装箱
// 返回拆箱的值到 'c'
char c = test('x');
escape sequence
A character preceded by a backslash (\) represents an escape character, which has a special meaning to the compiler.
The following list shows the escape sequences for Java:
escape sequence | describe |
---|---|
\t | Insert a tab key here in the text |
\b | Insert a back key here in the text |
\n | Newline here in the text |
\r | Insert a carriage return here in the text |
\f | Insert a page break here in the text |
\' | Insert single quotes here in the text |
\" | Insert double quotes here in the text |
\\ | Insert a backslash here in the text |
example
public class Main {
public static void main(String[] args) {
// 使用转义序列来插入特殊字符
System.out.println("Hello\tWorld"); // 在 Hello 和 World 之间插入一个制表符(tab)
System.out.println("Hello\bWorld"); // 在 Hello 后面插入一个后退键(backspace),删除了一个 'o'
System.out.println("Hello\nWorld"); // 在 Hello 后面插入一个换行符(newline),换到下一行输出
System.out.println("Hello\rWorld"); // 在 Hello 后面插入一个回车符(carriage return),将光标移到行首
System.out.println("Hello\fWorld"); // 在 Hello 和 World 之间插入一个换页符(form feed),清空屏幕并开始新一页
System.out.println("She said \'Hello\'"); // 在字符串中插入单引号,使用转义序列 \' 表示
System.out.println("He said \"Hello\""); // 在字符串中插入双引号,使用转义序列 \" 表示
System.out.println("C:\\Program Files\\Java"); // 在字符串中插入反斜杠,使用转义序列 \\ 表示两个反斜杠
}
}
The result of compiling and running the above example is as follows:
Hello World
HelloWorld
Hello
World
Hello
World
HelloWorld
She said 'Hello'
He said "Hello"
C:\Program Files\Java
Character method
The following are some common methods of the Character class:
isLetter(char ch)
: Checks if a character is a letter.isDigit(char ch)
: Checks if a character is a number.isWhitespace(char ch)
: Check if a character is a whitespace character, such as space, tab, newline, etc.isUpperCase(char ch)
: Checks if a character is an uppercase letter.isLowerCase(char ch)
: Checks if a character is a lowercase letter.toUpperCase(char ch)
: Convert characters to uppercase.toLowerCase(char ch)
: Convert characters to lowercase.toString(char ch)
: Convert a character to a string.isLetterOrDigit(char ch)
: Checks if a character is a letter or a number.isJavaIdentifierStart(char ch)
: Checks whether a character can be used as the starting character of a Java identifier.isJavaIdentifierPart(char ch)
: Checks if a character can be part of a Java identifier.digit(char ch, int radix)
: Convert the character to the corresponding integer value according to the specified base.compare(char x, char y)
: Compare the size of two characters.
For a complete list of methods, please refer to the java.lang.Character API specification.
example
public class Main {
public static void main(String[] args) {
char ch = 'A';
// 检查字符是否为字母
System.out.println(Character.isLetter(ch)); // 运行:true
// 检查字符是否为数字
System.out.println(Character.isDigit(ch)); // 运行:false
// 检查字符是否为空白字符
System.out.println(Character.isWhitespace(ch)); // 运行:false
// 检查字符是否为大写字母
System.out.println(Character.isUpperCase(ch)); // 运行:true
// 检查字符是否为小写字母
System.out.println(Character.isLowerCase(ch)); // 运行:false
// 将字符转换为大写形式
System.out.println(Character.toUpperCase(ch)); // 运行:A
// 将字符转换为小写形式
System.out.println(Character.toLowerCase(ch)); // 运行:a
// 将字符转换为字符串
System.out.println(Character.toString(ch)); // 运行:A
// 检查字符是否为字母或数字
System.out.println(Character.isLetterOrDigit(ch)); // 运行:true
// 检查字符是否可以作为 Java 标识符的起始字符
System.out.println(Character.isJavaIdentifierStart(ch)); // 运行:true
// 检查字符是否可以作为 Java 标识符的部分字符
System.out.println(Character.isJavaIdentifierPart(ch)); // 运行:true
// 将字符按照指定进制转换为对应的整数值
System.out.println(Character.digit(ch, 16)); // 运行:10
// 比较两个字符的大小
System.out.println(Character.compare('A', 'B')); // 运行:-1
}
}