微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 什么是点型铸造?

在这代码中:
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

原文地址:https://www.jb51.cc/java/121524.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐