[英]Basic arithmetic operations on int - Java
I recently noticed an idiosyncrasy of Java regarding basic arithmetic operations in Java. 我最近注意到Java在Java中的基本算术运算方面的特殊性。 With the following code 使用以下代码
byte a = 3;
byte b = 4;
byte c = a * b;
I get a "type mismatch" compilation error... 我收到“类型不匹配”编译错误...
Are basic arithmetic operations in Java ( +
, -
, *
, /
) only performed on primitive data types of int
and higher order ( long
, double
, etc.), whereas arithmetic operations on byte
and short
are first cast to int
and then evaluated? Java( +
, -
, *
, /
)中的基本算术运算是仅对int
和更高阶( long
, double
等)的基本数据类型执行的,而对byte
和short
算术运算首先转换为int
然后进行求值?
Operations on byte
, char
and short
are widened to int
unless the compiler can determine the value is in range. 除非编译器可以确定值在范围内,否则将byte
, char
和short
上的操作扩展为int
。
final byte a = 3, b = 4;
byte c = a * b; // compiles
final byte a = 3, b = 40;
byte c = a * b; // compiles
final int a = 3, b = 4;
byte c = a * b; // compiles !!
but 但
byte a = 3, b = 4;
byte c = a * b; // doesn't compile as the result of this will be `int` at runtime.
final byte a = 30, b = 40;
byte c = a * b; // doesn't compile as the value is too large, will be an `int`
BTW This compiles even though it results in an overflow. BTW即使导致溢出,也会编译。 :] :]
final int a = 300000, b = 400000;
int c = a * b; // compiles but overflows, is not made a `long`
The result of integer operations is either int
or long
. 整数运算的结果是int
或long
。 This is spelled out in the JLS : 这在JLS中有详细说明:
4.2.2. 4.2.2。 Integer Operations 整数运算
The numerical operators, which result in a value of type
int
orlong
: 数值运算符, 其结果为int
或long
类型的值 :
The unary plus and minus operators + and - (§15.15.3, §15.15.4) 一元加减运算符+和 - (§15.15.3,§15.15.4)
The multiplicative operators *, /, and % (§15.17) 乘法运算符*,/和%(§15.17)
The additive operators + and - (§15.18) 加法运算符+和 - (§15.18)
... ...
5.6.2. 5.6.2。 Binary Numeric Promotion 二进制数字促销
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order: 当运算符将二进制数字提升应用于一对操作数时,每个操作数必须表示可转换为数字类型的值,以下规则适用,顺序如下:
Widening primitive conversion (§5.1.2) is applied to convert either or both operands as specified by the following rules: 应用扩展基元转换(第5.1.2节)来转换由以下规则指定的一个或两个操作数:
If either operand is of type double, the other is converted to double. 如果任一操作数的类型为double,则另一个操作数转换为double。
Otherwise, if either operand is of type float, the other is converted to float. 否则,如果任一操作数的类型为float,则另一个操作数转换为float。
Otherwise, if either operand is of type long, the other is converted to long. 否则,如果任一操作数的类型为long,则另一个操作数转换为long。
Otherwise, both operands are converted to type int. 否则,两个操作数都将转换为int类型。
... ...
Binary numeric promotion is performed on the operands of certain operators: 对某些运算符的操作数执行二进制数字提升:
The multiplicative operators *, / and % (§15.17) 乘法运算符*,/和%(§15.17)
The addition and subtraction operators for numeric types + and - (§15.18.2) 数值类型的加法和减法运算符+和 - (§15.18.2)
The numerical comparison operators <, <=, >, and >= (§15.20.1) 数值比较运算符<,<=,>和> =(§15.20.1)
The numerical equality operators == and != (§15.21.1) 数值相等运算符==和!=(§15.21.1)
The integer bitwise operators &, ^, and | 整数位运算符&,^和| (§15.22.1) (§15.22.1)
In certain cases, the conditional operator ? 在某些情况下,条件运算符? : (§15.25) :(§15.25)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.