Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 C 中,我可以编写一个 if 语句
if (firstInt & 1)
但是当我尝试在 Java 中做同样的事情时,编译器告诉我“不兼容的类型”并说我需要 aboolean而不是int. 有没有办法用 Java 编写 C 代码?
boolean
int
以下任何一项都应该适合您:
if ((firstInt & 1) != 0) if ((firstInt & 1) > 0) if ((firstInt & 1) == 1)
在 C 中,整数表达式可以隐式用作布尔表达式(尽管我认为这是一个坏主意),其中零为假,任何非零值为真。
在 Java 中,这是不允许的,因此您必须通过使用比较运算符将整数结果与其他整数值或表达式进行比较,从而使表达式显式为布尔值。