img
int done;
// ...
if(!done) ... // Valid in C/C++
if(done) ...
// but not in Java.
In Java, these statements must be written like this:
if(done == 0) ... // This is Java-style.
if(done != 0) ...
The reason is that Java does not define true and false in the same way as C/C++. In C/C++,
true is any nonzero value and false is zero. In Java, true and false are nonnumeric values that
do not relate to zero or nonzero. Therefore, to test for zero or nonzero, you must explicitly
employ one or more of the relational operators.
Boolean Logical Operators
The Boolean logical operators shown here operate only on boolean operands. All of the
binary logical operators combine two boolean values to form a resultant boolean value.
Operator
Result
&
Logical AND
|
Logical OR
^
Logical XOR (exclusive OR)
||
Shor t-circuit OR
&&
Shor t-circuit AND
!
Logical unar y NOT
&=
AND assignment
|=
OR assignment
^=
XOR assignment
==
Equal to
!=
Not equal to
?:
Ternar y if-then-else
The logical Boolean operators, &, |, and ^, operate on boolean values in the same way
that they operate on the bits of an integer. The logical ! operator inverts the Boolean state:
!true == false and !false == true. The following table shows the effect of each logical operation:
A
B
A|B
A&B
A^B
!A
False
False
False
False
False
True
True
False
True
False
True
False
False
True
True
False
True
True
True
True
True
True
False
False
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home