Remark:

There are only eight basic types in Java, which are divided into seven numeric types and one non-numeric type (Boolean). Numeric types can be converted to each other. The seven numeric types can be divided into two piles, one for integer types and the other for floating point types. Floating-point types are the most common floats and doubles. The other five basic types can also be listed in order of byte length, short, char, int, and double.

Four of the five basic types listed above can be seen at a glance — int, short, int, and long. Why is char an integer?

1. Storage Angle

Each basic data type has its own length of bytes to hold a different number of values. Char is no exception. Char, like short, is two bytes long, or 16 bits long. Short is different from short: as we know, short is two bytes of 16 bits, which store between 2^-15 and 2^15-1, that is, between [-32768, +32767]. Char, on the other hand, only holds integers, and yes, because char is a character type, encoded in Unicode for the 16-bit Unicode character set (you never see negative Unicode characters, do you?). Since char is intended to represent positive numbers, it stores values between 0 and 2^16 (integers between [0, 65536] on the positive axis).

As a result, it is relatively easy to figure out the range of integers held by a char using 16-bit Unicode.

A char is a stored integer, just like the other four integer types. Just because of the need to represent characters, it is taken to store numbers in the positive range. However, the encoding is not the same, into the corresponding character.

With this understanding, it follows that char variables can also normally participate in arithmetic operations and assignments.

2, performance

The value of a char variable can be an integer:

    char a = 48;
    System.out.println("a = " + a);//a = 0
Copy the code

Char char char char char char char char char char char

    System.out.println(a+10);/ / 58
Copy the code

The reason:

In fact, the encoding of the character is involved in the operation.

Note: 48 corresponds to the digit 0 in the character set.

summary

Char is one of the integer types. It is two bytes long and corresponds to the Unicode character set with the same two-byte encoding. Holds the character values used to represent characters. Since there are no negative numbers in the Unicode character set, the corresponding bytes of char are used to hold positive numbers 0 and later.

2. The char variable performs the assignment, the arithmetic operation, and actually its encoded value.

Reference source

Crazy Java handouts 4th edition 3.4.2 Character types

Ps: Novice road, if there is any mistake or not precise place, please correct.