Java - char、int 转换
在 Java 中,允许以下内容:
In Java, the following is allowed:
char c = 'A' + 1;
在这里,c 将保存值B".上面,首先计算表达式.所以A"被转换为 65,整个表达式的计算结果为 66,然后 66 被转换为B",因为我们将值存储在一个字符中.
Here, c will hold the value 'B'. Above, first the expression is evaluated. So 'A' gets converted to 65, the whole expression evaluates to 66, and then 66 is converted to 'B' since we are storing the value in a char.
但是,以下给出了编译时错误:
The following, however, gives a compile-time error:
char c = 'A';
c = c + 1;
Java 如何以不同方式看待表达式的解释是什么?顺便说一句,以下内容也可以正常工作:
What is the explanation for how Java views the expressions differently? By the way, the following works fine too:
char c = 'A';
c++;
推荐答案
第一个例子(编译)是特殊的,因为加法的两个操作数都是文字.
The first example (which compiles) is special because both operands of the addition are literals.
一些定义开始:
将
int
转换为char
称为 缩小基元转换,因为char
是比int
.
Converting an
int
tochar
is called a narrowing primitive conversion, becausechar
is a smaller type thanint
.
'A' + 1
是 常量表达式.常量表达式(基本上)是结果始终相同并且可以在编译时确定的表达式.特别是,'A' + 1
是一个常量表达式,因为 +
的操作数都是文字.
'A' + 1
is a constant expression. A constant expression is (basically) an expression whose result is always the same and can be determined at compile-time. In particular, 'A' + 1
is a constant expression because the operands of +
are both literals.
在byte
、short
和 char
的赋值,if 赋值的右边是一个常量表达式:
A narrowing conversion is allowed during the assignments of byte
, short
and char
, if the right-hand side of the assignment is a constant expression:
另外,如果[右边的]表达式是byte
、short
、char
类型的常量表达式,或 int
:
In addition, if the expression [on the right-hand side] is a constant expression of type
byte
,short
,char
, orint
:
- 如果变量是
byte
、short
或char
类型,并且常量的值,则可以使用缩小原语转换表达式可以用变量的类型来表示.
- A narrowing primitive conversion may be used if the variable is of type
byte
,short
, orchar
, and the value of the constant expression is representable in the type of the variable.
c + 1
是 not 一个常量表达式,因为 c
是一个非final
变量,所以分配发生编译时错误.通过查看代码,我们可以确定结果总是相同的,但在这种情况下编译器不允许这样做.
c + 1
is not a constant expression, because c
is a non-final
variable, so a compile-time error occurs for the assignment. From looking at the code, we can determine that the result is always the same, but the compiler isn't allowed to do that in this case.
我们可以做的一件有趣的事情是:
One interesting thing we can do is this:
final char a = 'a';
char b = a + 1;
在那种情况下 a + 1
是 一个常量表达式,因为 a
是一个 final
变量,它是用常量表达式初始化.
In that case a + 1
is a constant expression, because a
is a final
variable which is initialized with a constant expression.
警告如果 […] 值 […] 可以用变量的类型表示";表示以下内容无法编译:
The caveat "if […] the value […] is representable in the type of the variable" means that the following would not compile:
char c = 'A' + 99999;
'A' + 99999
的值(即 100064
或 0x186E0
)太大而无法放入 char
,因为 char
是一个无符号的 16 位整数.
The value of 'A' + 99999
(which is 100064
, or 0x186E0
) is too big to fit in to a char
, because char
is an unsigned 16-bit integer.
至于后缀++
运算符:
后缀自增表达式的类型就是变量的类型.
The type of the postfix increment expression is the type of the variable.
...
在加法之前,对值1
和变量的值进行二进制数值提升*.如有必要,总和会通过缩小基元转换来缩小和/或在存储之前将其装箱转换为变量的类型.
Before the addition, binary numeric promotion* is performed on the value 1
and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion and/or subjected to boxing conversion to the type of the variable before it is stored.
(* 二进制数字Promotion 将 +
等运算符的 byte
、short
和 char
操作数转换为 int
或其他更大的类型.Java 不会对小于 int的整数类型进行算术运算代码>.)
(* Binary numeric promotion takes byte
, short
and char
operands of operators such as +
and converts them to int
or some other larger type. Java doesn't do arithmetic on integral types smaller than int
.)
换句话说,语句 c++;
大多等价于:
In other words, the statement c++;
is mostly equivalent to:
c = (char)(c + 1);
(不同的是表达式c++
的result,如果我们把它赋值给某个东西,就是c
的值在增量之前.)
(The difference is that the result of the expression c++
, if we assigned it to something, is the value of c
before the increment.)
其他增量和减量具有非常相似的规范.
The other increments and decrements have very similar specifications.
复合赋值运算符如+=
也会自动进行窄化转换,因此也允许使用诸如 c += 1
(甚至 c += 3.14
)之类的表达式.
Compound assignment operators such as +=
automatically perform narrowing conversion as well, so expressions such as c += 1
(or even c += 3.14
) are also allowed.
相关文章