Java – you can specify an integer value for char, but you cannot assign an integer variable to char

Why not compile the following code

int n = 5;
char c = n;

But the following compilation

char c = 5;

In both cases, don't I just assign an integer value to char?

Solution

Char can be assigned to int without cast because it is an extended cast To do the opposite, char's int needs to be cast because it is a reduced conversion

See also JLS Chapter 5. Conversions and Promotions.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>