In this chapter, we mainly introduce to you the Java Character class and the usage of the Character class.
When using characters, we usually use the built-in data type char.
char ch = 'a'; // Unicode for uppercase Greek omega character char uniChar = 'u039A'; // 字符数组char[] charArray = { 'a', 'b', 'c', 'd', 'e' };
However, in the actual development process, we often encounter situations where we need to use objects instead of built-in data types. In order to solve this problem, the Java language provides a wrapper class Character class for the built-in data type char.
Usage of the Character class: The Character class provides a series of methods to manipulate characters. You can use the Character constructor to create a Character class object, for example:
Character ch = new Character('a');
In some cases, the Java compiler automatically creates a Character object.
For example, when a char type parameter is passed to a character that requires a Character type parameter, the compiler will automatically convert the char type parameter into a Character object. This feature is called boxing, and the converse is called unboxing.
// Here following primitive char 'a' // is boxed into the Character object ch Character ch = 'a'; // Here primitive 'x' is boxed for method test, // return is unboxed to char 'c' char c = test('x');
The character preceded by a backslash () represents an escape character, which has special meaning to the compiler.
The following list shows Java's escape sequences:
escape sequence | describe |
---|---|
t | Insert a tab key here in the text |
b | Insert a back key here in the text |
n | Line break at this point in the text |
r | Insert a carriage return here in the text |
f | Insert a page break at that point in the text |
' | Insert single quotes here in the text |
" | Insert double quotes here in the text |
\ | Insert a backslash at this point in the text |
When a print statement encounters an escape sequence, the compiler interprets it correctly.
public class Test { public static void main(String args[]) { System.out.println("She said "Hello!" to me."); } }
The compilation and running results of the above example are as follows:
She said "Hello!" to me.
The following are the methods of the Character class:
serial number | Methods and Description |
---|---|
1 | isLetter() whether it is a letter |
2 | isDigit() whether it is a numeric character |
3 | isWhitespace() whether it is a space |
4 | isUpperCase() Is it an uppercase letter? |
5 | isLowerCase() whether it is lowercase letters |
6 | toUpperCase() specifies the uppercase form of letters |
7 | toLowerCase() specifies the lowercase form of letters |
8 | toString() returns the string form of the character. The length of the string is only 1 |
public static boolean isUpperCase(char ch): Determines whether the given character is an uppercase character;
public static boolean isLowerCase(char ch): Determines whether the given character is a lowercase character;
public static boolean isDigit(char ch): Determines whether the given character is a numeric character;
The boolean in these three sentences represents that the return value after using these three methods is of boolean type.
public class Java { public static void main(String[] args) { Character ch = new Character('X'); System.out.println(Character.isUpperCase(ch)); //Character.isUpperCase(ch) 用于判断括号里的字母是否为大写 System.out.println(Character.isLowerCase(ch)); //Character.isLowerCase(ch) 用于判断括号里的字母是否为小写 System.out.println(Character.isDigit(ch)); //Character.isDigit(ch) 用于判断括号里的内容是否为数字 } }
The running result is:
true false false
For a complete list of methods, please refer to the java.lang.Character API specification.