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

Detailed explanation of the Character class in Java

Y2J
Release: 2017-05-19 10:21:38
Original
3055 people have browsed it

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' };
Copy after login

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');
Copy after login

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("访问\"菜鸟教程!\"");   }}
Copy after login

The above example compiles and runs the results as follows:

访问"菜鸟教程!"
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template