Reputation: 2736
I just got the following compiler error:
./package/path/FrameScreenPosition.java:80: incompatible types
found : int
required: boolean
if (frame.getExtendedState() & Frame.MAXIMIZED_BOTH) {
^
An 'extended state' is a bitwise mask of various different states such as maximized or iconified, and I'm trying to test whether the frame is maximized. The following short example produces a similar error:
public class BitTest
{
public static void main(String[] args)
{
int a = 1;
int c = 3;
if (a & c) {
System.out.println("This is right.");
}
}
}
Everything I've seen suggests that the bitwise operator &
isn't restricted to boolean variables in Java, so why am I getting an error message?
Upvotes: 3
Views: 1065
Reputation: 129477
The expression inside if
must explicitly be a boolean (that is, boolean
or Boolean
-- see JLS §14.9 for further details):
if ((a & c) != 0) {
Also note that the second set of parenthesis are required here since !=
has a higher precedence than &
.
Upvotes: 15
Reputation: 29367
In Java if(..)
clause must always evaluate to a boolean
which is its own type unlike in, say, C.
So, 0
, null
etc. are not converted to boolean false
nor positive numbers to true
and so on and so forth. You must include arithmetic comparison operator to specify what you really want.
Upvotes: 3
Reputation: 4703
if (a & c) wont work here;
if(condition)
needs to have a boolean value.
Upvotes: 1