加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 什么是点型铸造?

发布时间:2020-12-14 05:44:44 所属栏目:Java 来源:网络整理
导读:在这段代码中: c = (char)(c - 'A' + 'a'); 为什么我们需要(char)?那是铸造权吗? 假设赋值语句右侧的c是大写字母.我假设我们在这里做Unicode加法和减法. 这是我正在阅读的Java书籍的片段: When arithmetic is done on a char,it is first converted to t
在这段代码中:
c = (char)(c - 'A' + 'a');

为什么我们需要(char)?那是铸造权吗?

假设赋值语句右侧的c是大写字母.我假设我们在这里做Unicode加法和减法.

这是我正在阅读的Java书籍的片段:

When arithmetic is done on a char,it is first converted to the int
that represents it in the Unicode system. Subtracting ’A’ from a
variable c essentially asks “How far into the upper-case letters is
the character in c?” Adding ’a’ then yields the int that is the same
distance into the sequence of lower-case alphabetic character code.
The cast to char is needed because char is a special kind of int with
a more limited range of values. By using the cast,the programmer
acknowledges that he or she understands the special nature of the
assignment and expects the value to be in the correct range,0 through
66535.

我不明白(char)的点?如果我们没有使用(char)演员,会有什么不同?什么是更一般的铸造?

解决方法

char是Java中的一个 integral type,当您执行算术时,结果是一个int( JLS-4.2.2. Integer Operations表示,部分是数值运算符,它导致int或long类型的值,并且包括加法运算符和 – ).
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读