Java - char、int 转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21317631/
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
Java - char, int conversions
提问by Cosmic_Dust
In Java, the following is allowed:
在 Java 中,允许以下内容:
char c = 'A' + 1;
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.
在这里,c 将保存值“B”。上面,首先计算表达式。因此,'A' 被转换为 65,整个表达式的计算结果为 66,然后 66 被转换为 'B',因为我们将值存储在一个字符中。
The following, however, gives a compile-time error:
但是,以下给出了编译时错误:
char c = 'A';
c = c + 1;
What is the explanation for how Java views the expressions differently?By the way, the following works fine too:
Java 如何以不同的方式看待表达式的解释是什么?顺便说一句,以下也可以正常工作:
char c = 'A';
c++;
采纳答案by Radiodef
The first example (which compiles) is special because both operands of the addition are literals.
第一个例子(编译)很特别,因为加法的两个操作数都是文字。
A few definitions to start with:
一些定义开始:
Converting an
int
tochar
is called a narrowing primitive conversion, becausechar
is a smaller type thanint
.'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.
一个转换
int
到char
被称为基本收缩转换,因为char
是比较小的类型int
。'A' + 1
是一个常量表达式。常量表达式(基本上)是结果始终相同的表达式,并且可以在编译时确定。特别是,'A' + 1
是一个常量表达式,因为 的操作数+
都是文字。
如果赋值的右侧是常量表达式,则在byte
、short
和的赋值期间允许进行收缩转换char
:
In addition, if the expression [on the right-hand side] is a constant expression of type
byte
,short
,char
, orint
:
- 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.
此外,如果表达式 [在右侧] 是类型为
byte
、short
、char
、 或的常量表达式int
:
- 如果变量的类型为
byte
、short
或char
,并且常量表达式的值可在变量的类型中表示,则可以使用缩小原语转换。
c + 1
is nota constant expression, because c
is a non-final
variable, so a compile-time error occurs for the assignment. From looking at the code, wecan determine that the result is always the same, but the compiler isn't allowed to do that in this case.
c + 1
是不是一个常量表达式,因为c
是非final
可变的,所以对于分配发生编译时间错误。通过查看代码,我们可以确定结果始终相同,但在这种情况下编译器不允许这样做。
One interesting thing we can do is this:
我们可以做的一件有趣的事情是:
final char a = 'a';
char b = a + 1;
In that case a + 1
isa constant expression, because a
is a final
variable which is initialized with a constant expression.
在那种情况下a + 1
是一个常量表达式,因为a
是一个final
用常量表达式初始化的变量。
The caveat "if […] the value […] is representable in the type of the variable" means that the following would not compile:
警告“如果 [...] 值 [...] 在变量的类型中是可表示的”意味着以下不会编译:
char c = 'A' + 99999;
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.
的值'A' + 99999
(其是100064
,或0x186E0
)太大,以适应于char
,因为char
是无符号16位整数。
As for the postfix ++
operator:
The type of the postfix increment expression is the type of the variable.
...
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 conversionand/or subjected to boxing conversion to the type of the variable before it is stored.
后缀增量表达式的类型是变量的类型。
...
在加法之前,对值
1
和变量的值进行二进制数值提升* 。如有必要,在存储之前,总和通过缩小原语转换和/或对变量类型进行装箱转换来缩小。
(* Binary numeric promotiontakes 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
.)
(*二进制数字提升采用byte
,short
和char
运算符的操作数,例如+
和 将它们转换为int
或其他更大的类型。Java 不会对小于int
. 的整数类型进行算术运算。)
In other words, the statement c++;
is mostly equivalent to:
换句话说,该语句c++;
大致相当于:
c = (char)(c + 1);
(The difference is that the resultof the expression c++
, if we assigned it to something, is the value of c
beforethe increment.)
(区别在于表达式的结果c++
,如果我们将它分配给某个东西,则是增量c
之前的值。)
The other increments and decrements have very similar specifications.
其他增量和减量具有非常相似的规格。
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.
复合赋值运算符(例如+=
自动执行收缩转换)也可以使用,因此也允许使用c += 1
(或 even c += 3.14
)等表达式。
回答by RMachnik
char to int conversion is called widening conversions. In widening conversions, values do not lose information about the overall magnitude of a numeric value where as int to char conversion is called narrowing conversions. With narrowing conversion you may lose information about the overall magnitude of a numeric value and may also lose precision.
char 到 int 的转换称为扩展转换。在扩大转换中,值不会丢失有关数值整体大小的信息,而 int 到 char 的转换称为缩小转换。通过缩小转换,您可能会丢失有关数值整体大小的信息,也可能会丢失精度。
For more information on primitive conversions refer thisdocument.
有关原始转换的更多信息,请参阅此文档。
回答by Brian
It is because the compiler can check that it ('A' + 1)
is within the bounds of a char whereas it cannot (in general) check that c + <an integer>
is within the bounds.
这是因为编译器可以检查它('A' + 1)
是否在字符的范围内,而它不能(通常)检查它c + <an integer>
是否在范围内。
回答by vinod bazari
Its because the literals for integer or smaller than int as byte ,short and char is int. Understand the following in this way.
这是因为整数或小于 int 作为 byte ,short 和 char 的文字是int。通过这种方式理解以下内容。
code:
代码:
byte a = 10;//compile fine
byte b= 11;//compile fine
byte c = a+b;//compiler error[says that result of **a+b** is **int**]
the same happens for any mathematical operations as of 'Divide', 'multiply', and other arithmetic operation. so cast the result to get the literal in desired data type
对于任何数学运算,如“除法”、“乘法”和其他算术运算,都会发生同样的情况。所以转换结果以获得所需数据类型的文字
byte c = (byte)(a+b);
so when you perform
所以当你表演
c= c+1;//compiler error
Its the result of c+1 is int not a char . so compiler give a compile time error for the same. so you need to provide a primitive cast to change the literal to char data type. Hope this example provide some understanding..
它的 c+1 的结果是 int 而不是 char 。所以编译器给出了相同的编译时错误。因此您需要提供一个原始类型转换来将文字更改为 char 数据类型。希望这个例子能提供一些理解..