Given the code fragment:
float x = 22.00f % 3.00f;
int y = 22 % 3;
System.out.print(x + ", " + y);
What is the result?
A.
1.0, 1
B.
1.0f, 1
C.
7.33, 7
D. Compilation fails
E. An exception is thrown at runtime
题解
「%」是余数操作符,支持整数和单倍精准浮点数(float)型态的余数计算。若操作数的其中一个为单倍精准浮点数型态,则计算出来的结果也会是单倍精准浮点数型态。
22 除以 3 的结果为 7 余 1,因此第 6 行会算出单倍精准浮点数型态的「1」,而第 7 行会算出整数型态的「1」。
