本文介绍了什么是点型铸造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此程式码片段中:

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

为什么我们需要(char) ?这是类型转换正确吗?

Why do we need the (char)? That's type casting right?

假设赋值语句右边的 c 是大写字母。我假设我们在这里进行Unicode加减法。

Assume the c on the right side of the assignment statement is a capital letter. I assume we're doing Unicode addition and subtraction here.

这是我读的Java书中的代码片段:

This is the snippet from the Java book that I'm reading:

我不明白(char) cast的点?如果我们不使用(char) cast,会有什么不同?

I don't understand the point of the (char) cast? What would be different if we didn't use the (char) cast? What is casting more generally?

推荐答案

char 是,当您执行算术时结果是 int (部分地说明数值运算符,其导致类型 int 或 long ,并添加添加运算符 + - )。

char is an integral type in Java, and when you perform arithmetic the result is an int (JLS-4.2.2. Integer Operations says, in part, the numerical operators, which result in a value of type int or long and adds that does include the additive operators + 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);

我得到

'A' = 65
'a' = 97

这篇关于什么是点型铸造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:40