负字符值JAVA [英] negative char Value JAVA

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

问题描述

为什么会发生以下情况:

Why doe it happen the following:

char p = 0;
p--;
System.out.println(p);

结果 65535

为什么不给出编译错误或运行时异常?
我期望它作为字符不能为负。相反,它开始从倒置计数。
提前感谢。

Why does not give it out a compilation error or a runtime Exception? I expected it as chars cannot be negative. Instead it starts back counting from upside down. Thanks in advance.

推荐答案


为什么不给出编译错误一个运行时异常?

Why does not give it out a compilation error or a runtime Exception?

因为语言规范要求原语类型的算术运算是 code>,因此 -1 变为 2 ^ 16-1

Because the language specification mandates that arithmetic on primitive types is modulo 2^width, so -1 becomes 2^16-1 as a char.

整数操作部分,表示


内置整数运算符不以任何方式指示上溢或下溢。

The built-in integer operators do not indicate overflow or underflow in any way.

,以便禁止抛出异常。

对于所使用的postfix-decrement运算符,具体来说,它的行为在 15.14.3

For the postfix-decrement operator used, specifically, its behaviour is specified in 15.14.3


否则,从变量值中减去值1并将该差存储回变量中。在减法之前,对值1和变量的值执行二进制数字提升(§5.6.2)。如有必要,通过缩小的原始转换(§5.1.3)和/或经过装箱转换(§5.1.7),在存储之前将差异缩小为变量的类型。后缀减量表达式的值是存储新值之前变量的值。

Otherwise, the value 1 is subtracted from the value of the variable and the difference is stored back into the variable. Before the subtraction, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the difference is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the postfix decrement expression is the value of the variable before the new value is stored.

二进制数字促销转换二者,值和1,到 int (因为这里的类型是 char ),因此你有中间结果 -1 作为 int ,则执行变窄原语转换:

The binary numeric promotion converts both, the value and 1, to int (since the type here is char), thus you have the intermediate result -1 as an int, then the narrowing primitive conversion is performed:


将有符号整数变换为整数类型T的过程简单地丢弃除了n个最低位之外的所有位,其中n是用于表示类型T的位数。除了可能的丢失有关数值大小的信息,这可能会导致结果值的符号与输入值的符号不同。

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 的值 0xFFFF (因为Java明确地指定了它的有符号整数类型的二进制补码表示在一元减号的说明中说明):

resulting in a char value of 0xFFFF (since Java specifies two's complement representation for its signed integer types, explicitly stated in the specification of unary minus):


对于整数值,否定与从零减法相同。 Java编程语言对整数使用二进制补码表示,二进制补码值的范围不是对称的,因此对最大负int或long的否定导致相同的最大负数。在这种情况下会发生溢出,但不会抛出异常。对于所有整数值x,-x等于(〜x)+1。

For integer values, negation is the same as subtraction from zero. The Java programming language uses two's-complement representation for integers, and the range of two's-complement values is not symmetric, so negation of the maximum negative int or long results in that same maximum negative number. Overflow occurs in this case, but no exception is thrown. For all integer values x, -x equals (~x)+1.

作为示例,在乘法运算符的规范中

For the general wrap-around behaviour for out-of-range results, as an example in the specification of the multiplication operator:


如果整数乘法溢出,则结果是数学乘积的低阶位足够大的二进制补码格式。因此,如果发生溢出,则结果的符号可能不与两个操作数值的数学积的符号相同。

If an integer multiplication overflows, then the result is the low-order bits of the mathematical product as represented in some sufficiently large two's-complement format. As a result, if overflow occurs, then the sign of the result may not be the same as the sign of the mathematical product of the two operand values.

类似的短语出现在整数加法的规范中,需要减法来满足 a - b == a +(-b)

Similar phrases occur in the specification of integer addition, and subtraction is required to fulfill a - b == a + (-b), so the overflow behaviour follows.

这篇关于负字符值JAVA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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