[Fixed] bad operand types for binary operator in java

In this post, we will see how to resolve bad operand types for binary operator & in java.

Table of Contents

Problem

Let’s first reproduce this issue with the help of simple example of Even odd program:

We will get error bad operand types for binary operator & at line 11.

Solution

We are getting this error due to precedence of the operators.

Since == has higher precedence than &, 1==0 will be evaluated first and will result in boolean. If you can notice, ‘&’ has two operands now – one boolean and another integer and it will result into compilation error due to different type of operands.

It is similar to why multiply is performed prior to addition.

You can fix this issue by using parenthesis properly.
Let’s fix this issue in above code snippet.

Output:

0

As you can see error is resolved now.

That’s all about how to resolve bad operand types for binary operator & in java

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *