Home  >  Article  >  Java  >  Detailed explanation of the Character class in Java

Detailed explanation of the Character class in Java

Y2J
Y2JOriginal
2017-05-19 10:21:383007browse

Character class is used to operate on single characters.

The Character class wraps a basic type char value in the 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 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 Character's constructor method 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 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

With a backslash (\) in front character represents an escape character, which has special meaning to the compiler.

The following list shows Java escape sequences:

Detailed explanation of the Character class in Java

Example

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

The following example escapes double quotes and outputs:

Test.java file code:

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

The above example compiles and runs the results as follows:

访问"菜鸟教程!"

Character method

The following are the methods of the Character class:

Detailed explanation of the Character class in Java

[Related recommendations]

1. Java free video tutorial

2. About the instance analysis of the packaging class Character

3. About the usage analysis of the Character class

4. Detailed explanation of instances of the Character class

5. Detailed explanation of the differences between Character and char methods

The above is the detailed content of Detailed explanation of the Character class in Java. For more information, please follow other related articles on the PHP Chinese website!

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