[英]In java why we can't assign int to char directly??? but vice-versa is true?
public class CharAndIntTest {
public static void main(String[] args) {
char i='1';
int ii=65;
i=ii;
}
}
An int
has larger capacity than a char
, so the conversion is not guaranteed to work. 一个int
容量大于一个char
容量,因此不能保证该转换有效。 The possible range in the value of a char
is 0 to 65535, but an int
can be anywhere from -2147483648 to 2147483647. char
值的可能范围是0到65535,但是int
可以在-2147483648到2147483647之间的任何地方。
However, there are other considerations other than raw storage size. 但是,除了原始存储大小外,还有其他注意事项。 char
is an unsigned type (Java's only built-in unsigned type, actually), so even short
and byte
variables still need a conversion to char
as they may be negative. char
是无符号类型(实际上是Java唯一的内置无符号类型),因此即使short
和byte
变量也可能需要转换为char
因为它们可能为负数。 short
and char
are both 16 bit, but since short
is signed its range is different from that of char
(range of short
being from -32,768 to 32,767). short
和char
均为16位,但由于short
是带符号的,因此其范围与char
范围不同( short
范围为-32,768至32,767)。
The most important thing to learn here is that Java only lets implicit conversions take place if it can be absolutely determined that the conversion will succeed. 在这里要学习的最重要的事情是, 如果可以绝对确定转换将成功 ,则Java仅允许进行隐式转换。 This applies to primitives, objects, generics, and pretty much everything. 这适用于原语,对象,泛型以及几乎所有内容。
You can force the conversion to happen using a cast: 您可以使用强制转换:
char i='1';
int ii=65;
i=(char)ii; //explicit cast tells the compiler to force the int into a char
The assignment conversion ( JLS, Section 5.2 ) allows a primitive widening conversion, but not a primitive narrowing conversion: 赋值转换( JLS,第5.2节 )允许原始的扩展转换,但不允许原始的缩小转换:
Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable. 当将表达式的值赋给变量时(第15.26节),将发生赋值转换:必须将表达式的类型转换为变量的类型。
Assignment contexts allow the use of one of the following: 分配上下文允许使用以下之一:
an identity conversion (§5.1.1) 身份转换(第5.1.1节)
a widening primitive conversion (§5.1.2) 不断扩大的原始转换(第5.1.2节)
a widening reference conversion (§5.1.5) 扩展参考转换(第5.1.5节)
a boxing conversion (§5.1.7) optionally followed by a widening reference conversion 装箱转换(第5.1.7节)(可选),然后进行扩大的参考转换
an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion. 拆箱转换(第5.1.8节)(可选),然后进行扩大的原始转换。
So you must cast to perform a primitive narrowing conversion, unless 因此,除非强制转换,否则必须执行原始的缩小转换
In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int: 此外,如果该表达式是类型为byte,short,char或int的常量表达式(第15.28节):
- 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. 如果变量的类型为byte,short或char,并且常量表达式的值可表示为变量的类型,则可以使用缩窄的原始转换。
So assigning a literal will work: 因此,分配文字将起作用:
char a = 65;
A char
is a 16-bit datatype while an int
is a 32-bit datatype. char
是16位数据类型,而int
是32位数据类型。 Thus, some possible int
values can't be represented with a char
without dropping some data (truncation). 因此,某些可能的int
值不能在不删除某些数据的情况下用char
表示(截断)。 The compiler creates an error message to warn you about the possible problems caused by accidental truncation. 编译器会创建一条错误消息,以警告您因意外截断而可能引起的问题。
You can still assign int
values to char
if you do the appropriate casting: 如果进行适当的转换,您仍然可以将int
值分配给char
:
int i=500;
char c = (char)i;
You can even assign illegal char
values like this, although the result will still be truncated: 您甚至可以像这样分配非法的char
值,尽管结果仍然会被截断:
int i = 65555;
char c = (char)i; //still compiles and doesn't cause an exception, but data is lost
i = c; //i's value is 19
char
s are 16-bit unsigned numbers, so they got a maximum of 65,635
. char
是16位无符号数字,因此最多可以有65,635
。
int
s are 32-bit signed numbers,so they got a maximum of 2,147,483,647
. int
是32位带符号的数字,因此它们的最大值为2,147,483,647
。
So something is bound to go wrong if you sign an int
to a char
, for all values >= 65,635
. 因此,如果您将一个int
签名为char
,并且所有值>= 65,635
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.