Java - char,int转换 [英] Java - char, int conversions

查看:214
本文介绍了Java - char,int转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,允许以下操作:

In Java, the following is allowed:

char c = 'A' + 1;

这里,c将保存值'B'。上面,首先评估表达式。因此,'A'被转换为65,整个表达式计算为66,然后66被转换为'B',因为我们将值存储在一个char。

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++;


推荐答案

将int转换为char是一个缩小的转换,'A'+ 1 是一个常量表达式。常数表达式(基本上)是其结果总是相同的表达式,并且可以由编译器确定。

It's just the spec. Converting an int to char is a narrowing conversion and 'A' + 1 is a constant expression. A constant expression is (basically) an expression whose result is always the same and can be determined by the compiler. Narrowing conversions are permitted for assignments of byte, short and char when it's a constant expression.

这里是 JLS转换说:


如果变量的类型为byte,short或char,并且常量表达式的值可以在变量类型中表示,则可以使用缩小的原始转换。

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.

c + 1 不是常量表达式,因此在赋值时会出现编译时错误。从查看代码,我们可以确定结果应该总是相同,但编译器无法确定它。

c + 1 is not a constant expression so a compile-time error occurs for the assignment. From looking at the code we can determine that the result should always be the same but the compiler cannot determine it.

我们可以do is this:

One interesting thing we can do is this:

final char a = 'a';
char b = a + 1;

在这种情况下, a + 1 因为 a

是最终的。

In that case a + 1 is a constant expression because a is final.

常量表达式的注意事项是,

The caveat to the constant expression is that the following would also not compile:

char c = 'A' + 99999;

因为'A'+ 99999 不能在char类型中表示。

Because the value of 'A' + 99999 is not representable in the type of char.

对于你提到的其他操作符,例如 ++ 他们有自己的规格。 ++的相关位 (类似地,所有的递增和递减)是:

As for other operators, like ++ that you mention, they have their own specifications. The relevant bit for ++ (and similarly all of the increments and decrements) is:


后缀的类型增量表达式是变量的类型。

The type of the postfix increment expression is the type of the variable.

...

§5.6.2)对值1和变量的值执行。如果需要,通过缩小的原始转换(§5.1.3)...将变量缩小为存储之前的变量类型。

Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) ... to the type of the variable before it is stored.

(可能是为char执行了转换。)还值得注意的是, + = 自动执行缩窄转换。所以 c + = 1; 也会编译。

(Presumably a conversion is done for char.) It's also worthwhile to note that += automatically performs narrowing conversion as well. So c += 1; will also compile.

这篇关于Java - char,int转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆