Java Character class


The Character class is used to operate on a single character.

Character class wraps a value of basic type char in an object

Instance

char ch = 'a';

// Unicode 字符表示形式
char uniChar = '\u039A'; 

// 字符数组
char[] charArray ={ 'a', 'b', 'c', 'd', 'e' };

However, in the actual development process, we often encounter To situations where you 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.

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 will automatically create a Character object.

For example, when a char type parameter is passed to a method 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.

Example

// 原始字符 'a' 装箱到 Character 对象 ch 中
Character ch = 'a';

// 原始字符 'x' 用 test 方法装箱
// 返回拆箱的值到 'c'
char c = test('x');

Escape sequence

The character preceded by a backslash (\) represents an escape character, which is special to the compiler meaning.

The following list shows Java escape sequences:

              Escape sequence               describe
\t               Insert a tab key
in the text \b               Insert a back key at this place in the text
          \n               Line break at this point in the text
            \r               Insert a carriage return
in the text \f               Insert a page break character
in the text \'             Insert single quotes
in the text \"             Insert double quotation marks
in the text \\               Insert backslash

Example

When a print statement encounters an escape sequence, the compiler can interpret it correctly.

The following example escapes double quotes and outputs:

public class Test {

   public static void main(String args[]) {
      System.out.println("访问\"php中文网!\"");
   }
}

The compilation and running results of the above example are as follows:

访问"php中文网!"

Character method

The following is Character Class method:

            Serial number             Methods and description
            1isLetter()
Is it a letter
            2isDigit()
Is it a numeric character
            3isWhitespace()
Is it a space
            4isUpperCase()
Is it a capital letter
            5isLowerCase()
Is it a lowercase letter
            6toUpperCase()
Specify the uppercase form of the letter
            7toLowerCase()
Specify the lowercase form of the letter
            8toString()
Returns the string form of the character, the length of the string is only 1

For a complete list of methods, please refer to the java.lang.Character API specification.