[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 &, bad operand types for binary operator &&, bad operand types for binary operator ==, 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


3. Bad operand types for binary operator ==

Example: Producing the error by using if condition

We will produce the error bad operand types for binary operator == first before moving on to the solution.

public class BadOperandTypes {
    public static void main(String args[]) {
      int x = 10;
      String y = "100";
      // Using == operator in if condition      
if (x == y)
{ System.out.println("Executing if block"); } else { System.out.println("Executing else block"); } } }


Output:
/BadOperandTypes.java:6: error: bad operand types for binary operator '=='
              if (x == y) {
                      ^
    first type: int
    second type: String
1 error

Explanation:

The cause of this error is due to the operands passed are of different types. If you notice == operator has two operands now, one is int, and the other is String. Since both operands are different it will give the compilation error as shown above.

Solution:

The above compilation error can be resolved by converting one of the operands to the same data types. If we convert int to String then the comparison will occur in lexicological order. Below, we are converting String to int.

public class BadOperandTypes {
    public static void main(String args[]) {
      int x = 10;
      String y = "100";
      // Using == operator in if condition      
if (x == Integer.parseInt(y))
{ System.out.println("Executing if block"); } else { System.out.println("Executing else block"); } } }


Output:
Executing else block


4. Bad operand types for binary operator <=

Example: Producing the error by using if condition

We will produce the error bad operand types for binary operator <= first before moving on to the solution.

public class BadOperandTypes2 {
    public static void main(String args[]) {
      int x = 5;
      String y = "500";
      // Using <= operator in if condition      
if (x <= y)
{ System.out.println("Executing if block"); } else { System.out.println("Executing else block"); } } }


Output:
/BadOperandTypes2.java:6: error: bad operand types for binary operator '<='
              if (x <= y) {
                      ^
    first type: int
    second type: String
1 error


Explanation:

Just like above, the cause of this error is due to the operands passed are of different types. If you notice <= operator has two operands now, one is int, and the other is String. Since both operands are different it will give the compilation error as shown above.

Solution:

The above compilation error can be resolved by converting one of the operands to the same data types. Please find below the modified code:

public class BadOperandTypes2 {
    public static void main(String args[]) {
      int x = 5;
      String y = "500";
      // Using <= operator in if condition      
if (x <= Integer.parseInt(y))
{ System.out.println("Executing if block"); } else { System.out.println("Executing else block"); } } }


Output:
Executing if block


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.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry