Java - 字符、整数转换 [英] Java - char, int conversions

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

问题描述

在 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.

一些定义开始:

  • Converting an int to char is called a narrowing primitive conversion, because char is a smaller type than int.

'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.

期间允许缩小转换byteshortchar的赋值,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:

另外,如果表达式[右侧]是byteshortchar类型的常量表达式,或 int:

In addition, if the expression [on the right-hand side] is a constant expression of type byte, short, char, or int:

  • 如果变量是 byteshortchar 类型的变量,以及常量的值,则可以使用缩小原语转换表达式可以用变量的类型来表示.
  • A narrowing primitive conversion may be used if the variable is of type byte, short, or char, and the value of the constant expression is representable in the type of the variable.

c + 1 不是一个常量表达式,因为 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(即 1000640x186E0)的值太大,无法放入 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.

至于postfix++ 运算符:

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

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.

(* 二进制数字Promotionbyteshortchar 运算符的操作数,例如+ 并将它们转换为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的值before 增量.)

(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.

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

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