Boolean expressions in Java

后端 未结 8 1065
不知归路
不知归路 2020-12-21 06:00

I have a question about the meaning (evaluation) of Boolean variables in return statements in Java.

I know that:

if (var) { ... }

i

8条回答
  •  囚心锁ツ
    2020-12-21 06:49

    A Java conditional requires a boolean value. If you can put it into an if statement, it's already a boolean, and requires no further fiddling if what you want is a boolean.

    Indeed, constructs like value == true can be tricky. I don't offhand remember the promotion rules in Java, but in C++ a bool can be promoted to an int, with false becoming 0 and true becoming 1. Therefore, int a = 2; if (a) and int a = 2; if (a == true) will do different things.

提交回复
热议问题