Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[Solved] bad operand types for binary operator in java

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

Many developers get confused with & bitwise AND operator and && logical AND operator. Both operators return true if all the conditions are true, if any of the given condition is false then they will return false.

When using boolean operands, the main difference between them is that && operator does not evaluate the next condition if the condition before it is false whereas & operator evaluates all conditions even if they are false.

First, we will produce the error before moving on to the solution.

Read Also: char cannot be dereferenced error in java

1. Bad operand types for binary operator &

Example 1: Producing the error by using if condition

We can easily produce this error by using & in the if condition as shown below:

public class JavaHungry {
public static void main(String args[]) {
int x=100;

// Using & operator in if condition
if(x & 100 == 1)
{
System.out.println("inside if condition");
}
else
{
System.out.println("inside else condition");
}
}
}

Output:
/JavaHungry.java:5: error: bad operand types for binary operator '&'
if(x & 100 == 1)
^
first type: int
second type: boolean
1 error


Explanation:

The cause of this error is due to the precedence of operators. == operator has higher precedence over & operator. As a result, 100==1 will be calculated first and return the boolean value. If you notice & operator has two operands now, one is int, and the other is boolean. Since both operands are different it will give the compilation error as shown above.

Solution:

The above compilation error can be resolved by using parenthesis properly.

public class JavaHungry {
public static void main(String args[]) {
int x=100;

// Using & operator in if condition
if((x & 100) == 1)
{
System.out.println("inside if condition");
}
else
{
System.out.println("inside else condition");
}
}
}


Output:
inside else condition

2. Bad operand types for binary operator &&

Example: Producing the error by using if condition

Just like above, we will produce the error first before moving on to the solution.

public class JavaHungry {
public static void main(String args[]) {
int x=1000;

if( (x > 100) && (x/2))
{
System.out.println(x);
}
}
}

Output:
/JavaHungry.java:5: error: bad operand types for binary operator '&&'
if( (x > 100) && (x/2))
^
first type: boolean
second type: int
1 error


Explanation:

The cause of this error is that (x/2) is a numeric expression that will return an integer value. We all know && is the logical AND operator. So, it expects boolean values on both sides. If you look at the if condition now, && operator has two operands: one is boolean and the other is int. The same is also mentioned in the compilation error.

Solution:

The above compilation error can be resolved by converting the second operand into a boolean type

public class JavaHungry {
public static void main(String args[]) {
int x=1000;
if( (x > 100) && (x/2 > 0))
{
System.out.println(x);
}
}
}

Output:
1000


That's all for today. Please mention in comments in case you are still facing the error bad operand types for binary operator in java.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

[Solved] bad operand types for binary operator in java

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×