HOWTO · Java

How to Fix the Bad Operand Types Error in Java

This article introduces the bad operand types error in Java, and how to fix it.

On this page

A Java bad operand types diagnostic is a compile-time error: the compiler cannot apply an operator to the types of the expressions on its left and right. Read the operator named in the message, then check the reported first and second types. Fix the expression according to what the program should mean; converting values just to silence the message can change the logic.

Java operators have different type rules. Bitwise & works with integral operands and also has a boolean form; logical && requires two boolean expressions. Equality == can compare compatible primitive values or references under Java’s equality rules. Relational <= compares numeric values. The examples below show common mismatches and corrected forms.

Bad Operand Types for the & Operator in Java

For integral operands, & computes a bitwise AND. Java also permits & between two booleans, where both sides are evaluated. An expression such as number & flag, with number an int and flag a boolean, is invalid because those operands do not select the same form of the operator. Choose the operation that matches the intent: use a bit mask with an integer, or use a boolean condition with boolean operands.

Operator precedence can produce the same diagnostic. In x & 21 == 1, equality binds more tightly than bitwise AND, so Java groups it as x & (21 == 1). The right operand is therefore boolean, which cannot be combined with the integer x in this expression. If the intent is to test whether the masked integer equals one, write (x & 21) == 1. Parentheses make the intended grouping clear.

Bad Operand Types for the && Operator in Java

The short-circuit logical operator && accepts a boolean expression on each side. It evaluates the right side only when the left side is true. A condition such as flag && number is invalid when number is an int; Java does not treat zero and nonzero integers as false and true. Convert the integer into a meaningful condition, for example flag && number > 0.

The same rule applies to arithmetic expressions. (x > 13) && (x * 3) has a boolean left side but an integer right side. If the intended test is that the product is positive, use (x > 13) && (x * 3 > 0). Pick a comparison that expresses the requirement rather than adding an arbitrary comparison merely to satisfy the compiler.

Bad Operand Types for the == Operator in Java

For primitive values, == tests value equality after permitted numeric conversions. For reference values, it tests whether the references identify the same object; it does not compare object contents. Java rejects comparisons for which neither rule defines a compatible pair. For example, int x = 23; String y = "23"; x == y cannot compile: an integer and a string are not directly comparable with ==.

If the string contains a number and numeric equality is intended, parse it first: x == Integer.parseInt(y). This converts the text to an integer before comparison. Integer.parseInt can throw NumberFormatException when the text is not a valid integer, so validate or handle input at the point where the value is read. If the intent is textual equality instead, compare strings with y.equals("23") (or Objects.equals when either reference may be null).

Bad Operand Types for the <= Operator in Java

The less-than-or-equal operator compares compatible numeric values; it does not order strings. Thus number <= text is invalid when number is an int and text is a String, even if the text looks numeric. When numeric ordering is intended, convert the text first, for example number <= Integer.parseInt(text). As with equality, parsing may fail for nonnumeric input. If the values are labels or other text, define the desired text-ordering behavior explicitly instead of applying a numeric comparison.

Run this example:

public class OperandTypesExample {
  public static void main(String[] args) {
    int x = 43;
    boolean maskMatches = (x & 21) == 1;
    boolean conditionMatches = (x > 13) && (x * 3 > 0);
    int value = 23;
    String text = "23";
    boolean valuesMatch = value == Integer.parseInt(text);
    boolean withinLimit = 5 <= Integer.parseInt("10");

    System.out.println("bitwise match: " + maskMatches);
    System.out.println("logical match: " + conditionMatches);
    System.out.println("numeric equality: " + valuesMatch);
    System.out.println("numeric ordering: " + withinLimit);
  }
}
bitwise match: true
logical match: true
numeric equality: true
numeric ordering: true

Use the operator and operand types named by the compiler to narrow the diagnosis. Then choose the corrected expression that represents the intended operation: mask integral values with &, build boolean conditions for &&, compare compatible values with ==, and parse numeric text before using <= when numeric order is what the program needs.