Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Character Class

Java Character Class

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

The Character class wraps a primitive type char value in an object

Example

char

ch

=

'

a

'

;

//

Unicode character representation

char

uniChar

=

'

\u

039A

'

;

//

Character array

char

[

]

charArray

=

{

'

a

'

,

'

b

'

,

'

c

'

,

'

d

'

,

'

e

'

}

;

However, in actual development, we often encounter situations where we need to use an object rather than a built-in data type. To solve this problem, the Java language provides the wrapper class Character for the built-in char data type.

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

Character

ch

=

new

Character

(

'

a

'

)

;

In some cases, the Java compiler automatically creates a Character object.

For example, when passing a char-type argument to a method that requires a Character-type parameter, the compiler automatically converts the char-type argument into a Character object. This feature is called boxing, and the reverse is called unboxing.

Example

//

Box the primitive character 'a' into the Character object ch

Character

ch

=

'

a

'

;

//

Box the primitive character 'x' with the test method

//

Return the unboxed value to 'c'

char

c

=

test

(

'

x

'

)

;


Escape sequences

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

The list below shows Java's escape sequences:

Escape sequences Description
\t Inserts a tab at that point in the text
\b Inserts a backspace at that point in the text
\n Inserts a newline at that point in the text
\r Inserts a carriage return at that point in the text
\f Inserts a form feed at that point in the text
' Inserts a single quote at that point in the text
" Inserts a double quote at that point in the text
\ Inserts a backslash at that point in the text

Example

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

The following example escapes double quotes and outputs:

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

[

]

args

)

{

System

.

out

.

println

(

"

Access the

"

Runoob Tutorial!

"

"

)

;

}

}

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


访问"Runoob Tutorial!"

Character Methods

The following are the methods of the Character class:

No. Method and Description
1 isLetter()
Whether it is a letter
2 isDigit()
Whether it is a digit character
3 isWhitespace()
Whether it is a whitespace character
4 isUpperCase()
Whether it is an uppercase letter
5 isLowerCase()
Whether it is a lowercase letter
6 toUpperCase()
The uppercase form of the specified letter
7 toLowerCase()
The lowercase form of the specified letter
8 toString()
Returns the string form of the character, with a length of only 1

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