Latest web development tutorials

Java Character class

Character classes are used to operate a single character.

Character class wraps a value in an object primitive type char

Examples

char ch = 'a';

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

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

However, in the actual development process, we often encounter the situation requires the use of an object, rather than built-in data types. To solve this problem, Java language provides built-in data type char wrapper classes Character classes.

Character class provides a series of methods to manipulate characters. You can create an object using the Character class constructor Character, for example:

Character ch = new Character('a');

In some cases, Java compiler will automatically create a Character object.

For example, when a parameter of type char is passed to a Character type parameter method, the compiler will automatically convert to the char type parameters Character object. This feature is called boxing, in turn, called unboxing.

Examples

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

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

Escape sequence

Preceded by a backslash (\) character represents the escape character, it is the compiler has a special meaning.

The following list shows the Java escape sequences:

Escape sequence description
\ T In the text where the insertion of a tab key
\ B In the text where the insertion of a back button
\ N Where in the text wrap
\ R Enter in the text where the insertion
\ F In the text where the insert page breaks
\ ' Inserted in the text where a single quote
\ " Inserted in the text where the double quotes
\\ Inserted in the text where the backslash

Examples

When you print a statement encounters an escape sequence, the compiler can be interpreted correctly.

The following examples are to escape the double quotes and outputs:

public class Test {

   public static void main(String args[]) {
      System.out.println("访问\"本教程!\"");
   }
}

The above examples compiled results are as follows:

访问"本教程!"

Character Method

Here is the Character class methods:

No. Method and Description
1 isLetter ()
Whether it is a letter
2 isDigit ()
Whether it is a numeric character
3 isWhitespace ()
Whether a space
4 isUpperCase ()
Whether it is uppercase
5 isLowerCase ()
It is lowercase letters
6 toUpperCase ()
Specifies uppercase letters
7 toLowerCase ()
Lowercase letters specified
8 toString ()
It returns the character string, the string length of only 1

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