Home > Java > Javagetting Started > body text

What is java char

藏色散人
Release: 2020-05-14 11:31:38
Original
5467 people have browsed it

What is java char

What is java char?

Char is a reserved word in Java. Unlike other languages, char is 16-bit in Java because Java uses Unicode. However, the 8-bit ASCII code is included in Unicode and ranges from 0 to 127.

But English, Spanish, German, and French don’t need to be expressed this way at all, so it is actually more efficient for them to use ASCII codes. There is a trade-off issue here.

Because char is 16 bits and uses Unicode encoding, char has the following initialization method:

//字符,可以是汉字,因为是Unicode编码
char c = 'c'; 
//可以用整数赋值
char c = 十进制数,八进制数,十六进制数等等;
//用字符的编码值来初始化
char c = '\u数字';
Copy after login

About how many bytes char occupies

1. "Byte" is byte, "bit" is bit;

2.1 byte = 8 bit;

char is 2 bytes in Java. Java uses unicode, 2 bytes (16 bits) to represent a character.

char type assignment

char a = 'a';  //任意单个字符,加单引号。
char a = '中';//任意单个中文字,加单引号。
char a = 111;//整数。0~65535。十进制、八进制、十六进制均可。输出字符编码表中对应的字符。
Copy after login

Note: char can only hold a single character.

Char operation

The char type can be operated on because char has corresponding values ​​in character encoding tables such as ASCII.

In Java, when running on char type characters, they are treated directly as integers corresponding to the ASCII table.

Example:

char m = 'a';
char m = 'a' + 'b';//char类型相加,提升为int类型,输出对应的字符。
int m = 'a' + 'b';//结果是195。//195没有超出int范围,直接输出195。
char m = 'a' + b;//会报错。//因为b是一个赋值的变量。
char m = 197;//输出字符编码表中对应的字符。
char m = '197';//会报错。//因为有单引号,表示是字符,只允许放单个字符。
char m = 'a' + 1;//输出结果是b。//提升为int,计算结果98对应的字符是b。
Copy after login

Summary

Use single quotation marks'' to identify, only a single character can be placed.

char char, char int - the types are promoted to int, and after appending the value to the char variable, the corresponding characters in the character encoding table are output.

For more java technical articles, please visit the java learning tutorial column!

The above is the detailed content of What is java char. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!