Java中带有字符和整数文字的整数算术

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3680625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:26:41  来源:igfitidea点击:

Integer arithmetic in Java with char and integer literal

java

提问by Ben

Can someone explain to me why the following code compiles OK in Java?

有人可以向我解释为什么以下代码在 Java 中编译正常吗?

char c = 'a' + 10;

Why is this not equivalent to the following, which does not compile?

为什么这不等同于以下不编译的内容?

int i = 10;
char c = 'a' + i;

The Java Language Specification (section 3.10.1) states "An integer literal is of type longif it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int(§4.2.1)." Section 4.2.2 refers to "The numerical operators, which result in a value of type intor long." So the result of the addition should, in my understanding, be an int, which cannot be assigned to the charvariable c.

Java 语言规范(第 3.10.1 节)指出“如果整数文字long以 ASCII 字母 L 或 l (ell) 为后缀,则它属于类型;否则它属于类型int(§4.2.1)。” 第 4.2.2 节提到“导致类型为intor的值的数字运算符long”。因此,在我看来,加法的结果应该是 an int,它不能分配给char变量c

However, it compiles fine (at least in Sun JDK 1.6.0 release 17 and in Eclipse Helios).

但是,它编译得很好(至少在 Sun JDK 1.6.0 release 17 和 Eclipse Helios 中)。

Rather an artificial example perhaps, but it is used in an introductory Java course I have been teaching, and it now occurs to me that I don't really understand why it works.

也许是一个人为的例子,但它被用在我一直在教授的 Java 入门课程中,现在我突然想起来我真的不明白它为什么有效。

采纳答案by Tom Hawtin - tackline

'a' + 10is a compile-time constant expressionwith the value of 'k', which can initialise a variable of type char. This is the same as being able to assign a bytevariable with a literal integer in [-128, 127]. A bytein the range of [128, 255] may be more annoying.

'a' + 10是一个编译时常量表达式,其值为'k',可以初始化类型为 的变量char。这与能够byte使用 [-128, 127] 中的文字整数分配变量相同。byte[128, 255] 范围内的A可能更烦人。

回答by einarmagnus

It is because the compiler can check that it ('a' + 10) is within the bounds of a char whereas it cannot (in general) check that 'a' + <an integer>is within the bounds.

这是因为编译器可以检查它 ( 'a' + 10) 是否在 char 的边界内,而它(通常)不能检查它'a' + <an integer>是否在边界内。

回答by Bob Fincheimer

The constant is of a different type (I know the spec says that 10should be an int, but the compiler doesn't see it that way).

常量是不同的类型(我知道规范说它10应该是一个 int,但编译器不这么认为)。

In char c = 'a' + 10, 10 is actually considered a constant variable of type char (so it can be added to a). Therefore char c = char + charworks.

在 中char c = 'a' + 10,10 实际上被认为是 char 类型的常量变量(因此可以将其添加到 a)。因此char c = char + char有效。

In int i = 10; char c = 'a' + i;You are adding a char to an integer (an integer can be much bigger than a char, so it chooses the bigger data type [int] to be the result a.k.a: 'a' + i = int + int). So the result of the addition is an integer, which cannot fit into the char c.

int i = 10; char c = 'a' + i;您将一个字符添加到一个整数(一个整数可以比一个字符大得多,因此它选择更大的数据类型 [ int] 作为结果又名:)'a' + i = int + int。所以加法的结果是一个整数,不能放入char c.

If you explicitly casted ito be a char (e.g.: char c = 'a' + (char)i;) it could work or if you did the opposite (e.g.: int c = (int)'a' + i;) it would work.

如果您显式地i转换为字符(例如:)char c = 'a' + (char)i;它可以工作,或者如果您做相反的事情(例如:)int c = (int)'a' + i;它会工作。

回答by Po Chen

char is actually an unsigned 16-bit integer with a range 0-65535. So you can assign any integer literal in that range to a char, e.g., "char c = 96", which results in "c" holding the character "a". You can print out the result using System.out.println(c).

char 实际上是一个无符号的 16 位整数,范围为 0-65535。因此,您可以将该范围内的任何整数文字分配给一个字符,例如,“char c = 96”,这将导致“c”包含字符“a”。您可以使用 System.out.println(c) 打印出结果。

For the constant expression on the right-hand-side of "char c = 'a' + 10", 'a' is promoted to int first because of the Java numeric promotion rules and the integer value is 96. After adding 10 to it, we get a literal integer 106, which can be assigned to a char.

对于"char c = 'a' + 10"右边的常量表达式,由于Java数值提升规则,'a'先提升为int,整数值为96,加10后,我们得到一个文字整数 106,它可以分配给一个字符。

The right-hand-side of "char c = 'a' + i" is not a constant expression and the expression result assignment rule requires an explicit cast from int to char, i.e., "char c = (char) ('a' + i)".

"char c = 'a' + i" 的右侧不是常量表达式,表达式结果赋值规则要求从 int 显式转换为 char,即 "char c = (char) ('a' + i)”。