Free: 4 trials of 20 minutes, no cardStart free

Craftstack Academy
Java

bad operand types for binary operator (Java fix)

· 13 min read

You write an if that reads perfectly well in English, press compile, and Java stops you:

Terminal
Main.java:5: error: bad operand types for binary operator '&&'
        if (hours > 2 && name) {
                      ^
  first type:  boolean
  second type: String
1 error

bad operand types for binary operator is a compile error: Java found it before your program ran a single line. The message already holds almost everything you need. It names the operator, and the two lines below it name the types of the values on each side. This article shows how to read those three clues, the five causes behind nearly every case, and the fix for each.

Try it yourself

Save this as Main.java, then run javac Main.java in the same folder:

Main.java
public class Main {
    public static void main(String[] args) {
        int hours = 3;
        String name = "Alex";
        if (hours > 2 && name) {
            System.out.println("ok");
        }
    }
}

You'll get exactly the error above. Keep the file open: you'll change one line at a time as you read on.

What "bad operand types for binary operator" means

An operator is a symbol like +, <, && or ==. A binary operator takes two values, one on each side, and those values are its operands. The error means: the operator was given two values whose types it can't work with. Java checks this at compile time, so the program never starts.

Read the message in three parts:

  • '&&': the operator that refused. The ^ mark under the code points at it.
  • first type: boolean: the type of the value on the left of the operator.
  • second type: String: the type of the value on the right.

Now ask the question that turns the error into a clue: does this operator make sense for these two types? && means "and". It joins two true/false values. hours > 2 is a boolean, but name is a String. "True and Alex" has no meaning, so Java stops.

Cause 1: a condition that isn't true or false

The operators && (and), || (or) and ! (not) only work on boolean values. In English we shorten sentences all the time, and code written the same way breaks:

java
String answer = "yes";
if (answer == "yes" && 3) {
    System.out.println("ok");
}
Terminal
Main.java:4: error: bad operand types for binary operator '&&'
        if (answer == "yes" && 3) {
                            ^
  first type:  boolean
  second type: int
1 error

The left side is a comparison, so it's a boolean. The right side is the number 3. Some languages treat any non-zero number as "true", but Java doesn't: a number is never a condition.

The fix is to write a full comparison on both sides of &&:

java
if (hours > 2 && name.equals("Alex")) {
    System.out.println("ok");
}

Each half is now a question with a yes-or-no answer.

The "between two numbers" version

A close relative is the "between" check, written the way we'd say it out loud:

java
int age = 25;
if (age > 18 && < 65) {
    System.out.println("working age");
}

This one doesn't even reach the type check:

Terminal
Main.java:4: error: illegal start of type
        if (age > 18 && < 65) {
                          ^
Main.java:4: error: ';' expected
        if (age > 18 && < 65) {
                            ^
2 errors

The cause is the same: < 65 isn't a complete condition. Repeat the variable: if (age > 18 && age < 65).

Cause 2: comparing text with < or >

This is the most common case for beginners, and it often starts with user input. You ask how many steps someone walked today and compare the answer with a goal:

Main.java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Steps today? ");
        String steps = scanner.nextLine();
        if (steps >= 10000) {
            System.out.println("Goal reached");
        } else {
            System.out.println("Keep walking");
        }
    }
}
Terminal
Main.java:8: error: bad operand types for binary operator '>='
        if (steps >= 10000) {
                  ^
  first type:  String
  second type: int
1 error

first type: String is the whole story. nextLine() always gives back text, even when the user types only digits. The comparison operators <, >, <= and >= only work on numbers (and on char, which Java stores as a number).

The fix: convert the text first

Turn the text into a number with Integer.parseInt before you compare:

Main.java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Steps today? ");
        int steps = Integer.parseInt(scanner.nextLine().trim());
        if (steps >= 10000) {
            System.out.println("Goal reached");
        } else {
            System.out.println("Keep walking: " + (10000 - steps) + " steps to go");
        }
    }
}
Terminal
Steps today? 8000
Keep walking: 2000 steps to go

trim() removes any spaces around the number, so an answer like 8000 still works. For decimals, use double and Double.parseDouble. If the user might type a word instead of a number, read String cannot be converted to int: it shows how to catch the NumberFormatException.

Why not compare the text directly?

Strings do have a way to be ordered: compareTo. But it compares alphabetically, character by character, not by numeric value:

java
String a = "8000";
String b = "10000";
System.out.println(a.compareTo(b) < 0);
Terminal
false

By numbers, 8000 is smaller than 10000. By characters, "8" comes after "1", so "8000" sorts after "10000". When the text holds a number, convert it. Use compareTo only when you really want alphabetical order, for example to sort names.

Cause 3: arithmetic on text

The same idea applies to *, -, / and %. They work on numbers only:

java
String price = "4";
int total = price * 3;
Terminal
Main.java:4: error: bad operand types for binary operator '*'
        int total = price * 3;
                          ^
  first type:  String
  second type: int
1 error

Fix it the same way: int total = Integer.parseInt(price) * 3;, or store the price as a number from the start.

Two strings fail too. Java has no "minus" for text:

java
String first = "Alex";
String last = "Smith";
String result = first - last;
Terminal
Main.java:5: error: bad operand types for binary operator '-'
        String result = first - last;
                              ^
  first type:  String
  second type: String
1 error

Only + works with a String, and it joins rather than adds: "4" + 3 gives the text "43". That's why + almost never causes this error with strings, and why it can give a surprising result instead of an error.

Cause 4: objects that Java can't compare with < or +

Comparison and arithmetic operators work on Java's primitive number types (int, long, double and the rest) and on their wrapper classes like Integer and Double. They don't work on other objects, even objects that have an obvious order, like dates:

java
LocalDate today = LocalDate.of(2026, 11, 17);
LocalDate deadline = LocalDate.of(2026, 12, 1);
if (today < deadline) {
    System.out.println("still time");
}
Terminal
Main.java:6: error: bad operand types for binary operator '<'
        if (today < deadline) {
                  ^
  first type:  LocalDate
  second type: LocalDate
1 error

Objects use methods for this. Dates have isBefore and isAfter, and many classes have compareTo:

java
if (today.isBefore(deadline)) {
    System.out.println("still time");
}

The same happens with an array or a list. int[] is an object, not a number:

java
int[] scores = {3, 5};
int total = scores + 1;
Terminal
Main.java:4: error: bad operand types for binary operator '+'
        int total = scores + 1;
                           ^
  first type:  int[]
  second type: int
1 error

You probably meant one element, like scores[0] + 1, or the sum of all of them, which needs a loop.

Cause 5: a boolean where a number is expected

boolean in Java is not a number. Unlike some languages, true isn't 1:

java
boolean flag = true;
int bad = flag + 1;
Terminal
Main.java:4: error: bad operand types for binary operator '+'
        int bad = flag + 1;
                       ^
  first type:  boolean
  second type: int
1 error

If you want to count true values, write it out: int value = flag ? 1 : 0;.

Variants of the message

The same error appears with every operator. These are the ones beginners search for most, all with the same reading method: operator, first type, second type.

Terminal
error: bad operand types for binary operator '&&'
error: bad operand types for binary operator '||'
error: bad operand types for binary operator '<'
error: bad operand types for binary operator '>='
error: bad operand types for binary operator '*'
error: bad operand types for binary operator '-'
error: bad operand types for binary operator '+'
error: bad operand types for binary operator '&'

Your editor may word it a little differently. IntelliJ IDEA, for example, shows the build output with a java: prefix, such as java: bad operand types for binary operator '>='. It's the same compiler message.

The unary version: bad operand type int for unary operator '!'

A unary operator takes one value. ! (not) is the common one:

java
int a = 3;
if (!a) {
    System.out.println("zero");
}
Terminal
Main.java:4: error: bad operand type int for unary operator '!'
        if (!a) {
            ^
1 error

Note the singular "type": there's only one operand. The fix is to ask a real yes-or-no question: if (a == 0).

Errors that look alike

incomparable types: boolean and int

When the operator is == or != and the two types can never be equal, Java uses a different word:

java
boolean adult = age >= 18;
if (adult == 1) {
    System.out.println("adult");
}
Terminal
Main.java:5: error: incomparable types: boolean and int
        if (adult == 1) {
                  ^
1 error

Write if (adult). A boolean is already a condition. The same message appears for a == b == c with three numbers: a == b becomes a boolean, and Java then tries to compare that with c. Write a == b && b == c.

Comparing a String with a char using == gives the "bad operand types" wording again (first type: String, second type: char). Use answer.equals("y") or answer.charAt(0) == 'y'.

incompatible types: int cannot be converted to boolean

A single = inside an if is an assignment, not a comparison:

java
int x = 5;
if (x = 5) {
    System.out.println("five");
}
Terminal
Main.java:4: error: incompatible types: int cannot be converted to boolean
        if (x = 5) {
              ^
1 error

Use == to compare. This error family, "incompatible types", is explained in String cannot be converted to int.

The one that gives no error: ^ is not "to the power of"

^ compiles happily with two ints, which makes it more dangerous than the errors above:

java
int side = 4;
System.out.println(side ^ 2);
System.out.println(Math.pow(side, 2));
System.out.println(side * side);
Terminal
6
16.0
16

In Java, ^ is "exclusive or", a bit operation. For powers, use Math.pow (it returns a double) or multiply.

Wrong fixes that don't help

  • Casting the String: (int) steps. A cast can't turn text into a number. Java answers with incompatible types: String cannot be converted to int. Use Integer.parseInt.
  • Comparing strings with == to avoid the error. answer == "yes" compiles, but it checks whether two variables point to the same object, not whether the text is equal. In a quick test, a String built at runtime gave false for == "yes" and true for .equals("yes"). Always use .equals for text.
  • Changing the variable to Object. The error moves, it doesn't go away: first type: Object, second type: int.

A debugging walkthrough

Here's a realistic beginner program that fails, and the order to look at things:

java
String input = scanner.nextLine();
if (input > 0 && input < 100) { ... }
  1. Read the operator in the message: '>'. It's a comparison, so it needs numbers.
  2. Read first type: String. So input is text.
  3. Find where input got its value: scanner.nextLine(), which always returns a String.
  4. Convert once, near the top: int value = Integer.parseInt(input.trim());
  5. Use the number everywhere after that: if (value > 0 && value < 100).

Converting once, as soon as the text arrives, is a good habit. Every later line then works with the right type.

Practice

Predict the error, or the output, before you run each one.

  1. double price = 2.5; String label = "€"; System.out.println(price * label);
  2. boolean ready = true; if (ready && "go") { }
  3. int a = 7; System.out.println(a ^ 2);

Answers. (1) bad operand types for binary operator '*', with first type: double and second type: String. You probably wanted label + price. (2) '&&' with boolean and String: compare the text with .equals. (3) No error: it prints 5, because ^ is exclusive or. For 49, use a * a.

Frequently asked questions

What does "first type" and "second type" mean?

They're the types of the values on the left and right of the operator. The first type belongs to the value before the operator, the second to the value after it.

Why does this work in Python or JavaScript but not in Java?

Those languages decide types while the program runs and sometimes convert values for you. Java checks every type before running, and it never converts text to a number on its own. That's stricter, but you find the mistake earlier.

How do I compare two Strings in Java?

Use a.equals(b) for "is the text the same", and a.compareTo(b) for alphabetical order (a negative result means a comes first).

How do I compare two dates or objects?

Use their methods: isBefore, isAfter and equals for dates, compareTo for most classes that have an order.

Is this a compile error or a runtime error?

A compile error. javac stops, and no .class file is created, so nothing runs until you fix it.

A quick checklist

  1. Read the operator in quotes and point at it in your code.
  2. Read first type and second type: the left and the right value.
  3. Ask: does this operator make sense for these two types?
  4. For &&, || and !, make each side a full true/false condition.
  5. For <, >, *, - on text, convert with Integer.parseInt or Double.parseDouble first.
  6. For objects like dates, use a method (isBefore, compareTo) instead of an operator.

Reading errors this way works for every language, not only Java. See how to read an error message for the general method, and Java NullPointerException for the error you'll probably meet next. The official Java tutorial on operators lists which operators work on which types.

1 beginner error a week

One common error, what it means and how to fix it, in a short email. Free, and you can unsubscribe in any email.

Learn by writing the code yourself.

Twenty minutes with the tutor, right in your browser. No card, nothing to install.

Start the free trial
A session: the tutor's chat on the left, the code editor with a Java program and its output on the right