为什么我可以生成超出最大值的字符?
Why can I generate char beyond max value?
为什么下面的代码不会产生错误?
System.out.println((char) 2147483647);
根据 oracle datatypes,char
的最大大小为 65,535
。
- char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
实际上,您问题的答案是您不能。
为什么你没有看到任何错误?
因为 Character.MAX_VALUE + 1 = Character.MIN_VALUE
... 与 Integer.MAX_VALUE
和其他人一样,JVM
将其视为一个循环以避免此类问题... 但是在尝试时给出错误的结果计算.....
查看 this question 了解更多技术信息
这适用于任何数据类型,它被称为 integer overflow
并且在转换任何超出您要转换为的类型的大小的位时被截断。
您进行了从 int
到 char
的缩小转换,这是允许的 see java spec: 5.1.3. Narrowing Primitive Conversion:
A narrowing conversion of a signed integer to an integral type T
simply discards all but the n lowest order bits, where n is the number of bits used to represent type T
. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.
结果 char
不大于 Character.MAX_VALUE
。
编译器将 (char) 2147483647
转换为 65535
Why can I generate char beyond max value?
2147483647
不是 char
,而是 int
。
您没有为 char
分配无效值,您正在将有效的 int
转换为 char
,然后 缩小基元转换 规则适用。参见 Java specs:§5.1.3。
简而言之,您保留原始整数的最低 16 位 ("A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.")。
Why does the following code not generate an error?
因为这不是错误,所以它是定义明确的行为。
char
是一种 16 位数据类型,可以将数值转换为该类型。
当您将 32 位整数转换为 16 位短整型(或转换为 char)时,您丢失了源的高位。
Int32 i = 2147483647; // 0x7FFFFFFF
Int16 s = (Int16) i; // 0xFFFF (65535)
char c = (char) i; // 0xFFFF (65535)
为什么下面的代码不会产生错误?
System.out.println((char) 2147483647);
根据 oracle datatypes,char
的最大大小为 65,535
。
- char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
实际上,您问题的答案是您不能。
为什么你没有看到任何错误?
因为 Character.MAX_VALUE + 1 = Character.MIN_VALUE
... 与 Integer.MAX_VALUE
和其他人一样,JVM
将其视为一个循环以避免此类问题... 但是在尝试时给出错误的结果计算.....
查看 this question 了解更多技术信息
这适用于任何数据类型,它被称为 integer overflow
并且在转换任何超出您要转换为的类型的大小的位时被截断。
您进行了从 int
到 char
的缩小转换,这是允许的 see java spec: 5.1.3. Narrowing Primitive Conversion:
A narrowing conversion of a signed integer to an integral type
T
simply discards all but the n lowest order bits, where n is the number of bits used to represent typeT
. In addition to a possible loss of information about the magnitude of the numeric value, this may cause the sign of the resulting value to differ from the sign of the input value.
结果 char
不大于 Character.MAX_VALUE
。
编译器将 (char) 2147483647
转换为 65535
Why can I generate char beyond max value?
2147483647
不是 char
,而是 int
。
您没有为 char
分配无效值,您正在将有效的 int
转换为 char
,然后 缩小基元转换 规则适用。参见 Java specs:§5.1.3。
简而言之,您保留原始整数的最低 16 位 ("A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T.")。
Why does the following code not generate an error?
因为这不是错误,所以它是定义明确的行为。
char
是一种 16 位数据类型,可以将数值转换为该类型。
当您将 32 位整数转换为 16 位短整型(或转换为 char)时,您丢失了源的高位。
Int32 i = 2147483647; // 0x7FFFFFFF
Int16 s = (Int16) i; // 0xFFFF (65535)
char c = (char) i; // 0xFFFF (65535)