Java – what is dot casting?

In this Code:

c = (char)(c - 'A' + 'a');

Why do we need a char? Is that foundry right?

Suppose that C on the right side of the assignment statement is an uppercase letter I suppose we do Unicode addition and subtraction here

This is a fragment of the java book I'm reading:

I don't understand the point of (char)? What would be the difference if we didn't use (char) actors? What is more general casting?

Solution

Char is an integral type in Java. When you perform arithmetic, the result is an int (jls-4.2.2. Integer operations indicates that part of it is a numeric operator, which results in a value of type int or long, including the addition operator and –)

char c = 'A';
System.out.printf("'%c' = %d%n",c,(int) c);
int d = (c - 'A' + 'a'); // c - 65 + 97
System.out.printf("'%c' = %d%n",(char) d,d);

I got it

'A' = 65
'a' = 97
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
分享
二维码
< <上一篇
下一篇>>