Why can a variable of type char in Java be assigned an integer number


Article source: link


1. In JAVA, char occupies 2 bytes and 16 bits. Can store Chinese characters

2. Char assignment

char a='a'; //Any single character, add single quotes.

char a='中';//Any single Chinese character, add single quotes.

char a=111;//Integer. 0~65535. Decimal, octal, or hexadecimal can be used. Output the corresponding characters in the character encoding table.

Note: Only a single character can be put.

3. Char operation

The char type is operable because char has corresponding values ​​in character encoding tables such as ASCII.

In JAVA, when the char type character is run, it is directly treated as an integer corresponding to the ASCII table.


Example 1:
char m='a'; -->a
char m='a'+'b'; -->? //Char type is added, promoted to int type, and the corresponding characters are output (a and b respectively The corresponding values ​​are 97 and 98, and the sum is equal to 195. The corresponding character is?).
int m='a'+'b'; -->195 //195 does not exceed the range of int, directly output 195.
char m=197; -->? //Output the corresponding characters in the character code table.
char m='197; -->An error is reported.//Because there are single quotes, it means that it is a character, and only a single character is allowed.
char m='a'+1; -->b //Promote to int, the character corresponding to the calculation result 98 is b.
char m='中'+'country'; -->42282
char m='中'+'country'+'country'+'country'; -->An error is reported//int to char is lost. Because the result has exceeded the range of the char type.
int m='中'+'国'+'国'+'国'; -->86820
char m='中'+1; -->丮//1 is int, the result is promoted to int, and the corresponding output character.
char m='中'+"Country"; -->An error is reported //String cannot be converted to char.
System.out.println('中'+"国"); -->China//There is no variable value process. String is connected to any character with "+" and converted to String.


Example 2:

char a = 97; -->Assign a constant value of 97 to the char type variable a.
char b ='a'+3; -->d // 97+3=100, the character corresponding to ASCII is d.
char c = a+3; --> an error is reported.//Cannot convert from int type to char type. Next, let us understand why we can’t do this operation:
First, we first know that in jvm memory mechanism, char type data operation is The integer corresponding to the character in the ASCII table is used in the operation in the int type (it can be considered as'a
'=97), the constant (97) and the constant (3) are operated to obtain a new constant (100), and the constant is assigned to the variable (b), There is no forced conversion, as long as the type range of the accepted variable (b) is greater than the constant. The variable declaration needs to define the data type (for example: char
a), and the memory divides a space of char type size for this variable, where the value of the variable (a) is variable, and the value of the constant (3) is unchanged Yes, the two operations are still a variable, in this example (a+3) is a variable of type int, and the assignment of a variable of int type (a+3) to a variable of char type (c) requires a coercion, so an error will be reported.

char c=(char)(a+3); -->d


to sum up:

Use a single quotation mark '' to identify, only a single character can be put.
char+char, char+int-the types are all promoted to int, after attaching the char variable, the corresponding character in the character encoding table is output.

Why can a variable of type char be assigned an integer number?

Char in Java is unicode encoding
. For char data in Java, what is stored in memory is an integer, which corresponds to an ASCII code table. For example, the character “A” corresponds to 65
characters and “a” corresponds to 97. Dao So char c1 ='A'; is equivalent to char c1 = 65;
because of this, character data can be used as a character or as an integer, so char data can be operated on with int data !
For example, the result of'A' + 1 is'B' if it is received with a char variable, and 66 is received with an int variable.
Char c ='A'+1; Finally, c is'B'
int i ='A' + 1; Finally, i is 66.
Although the char type is stored as an integer, there is still a certain difference from the int type. The value range of the
char type is 2 bytes. The unsigned range is 0~65535

So char c = 65536 will report an error, just because it crossed the boundary

Guess you like

Origin blog.csdn.net/weixin_42656358/article/details/108443297