Home  >  Article  >  Java  >  [java tutorial] Java Character class

[java tutorial] Java Character class

黄舟
黄舟Original
2016-12-26 13:06:36949browse

Java Character Class

When using characters, we usually use the built-in data type char.

Examples

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. 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

// 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');

Escape sequence

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 Sequences

Description

t Insert a tab key in the text

b Insert a back key in the text

n Insert a line break in the text

r Insert a carriage return in the text

f Insert a page break in the text

' Insert a single quotation mark in the text

" Insert a double quotation mark in the text

\ Insert backslash in the text

Example

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

public class Test {

   public static void main(String args[]) {
      System.out.println("She said \"Hello!\" to me.");
   }
}

The above. The example compilation and running results are as follows:

She said "Hello!" to me.

Character method

The following are the methods of the Character class:

Serial number

Method and description

1 isLetter()
Is it a letter

2 isDigit()
Is it a numeric character

3 isWhitespace( )
is a space

4 isUpperCase()
is an uppercase letter

5 isLowerCase()
is a lowercase letter

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 string The length is only 1

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

The above is the content of [java tutorial] Java Character class, more For related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn