Java is also one of the most popular and oldest programming languages in the technology world. Java has the ability to apply for back-end, front-end, software development, etc. Operators play an important role in programming, and sometimes, a binary operator can give a bad operand type error due to improper use. The bad operand type error happens when both types of operands are conflicting. In this article, we’ll discuss why we may meet the Java compile-time error “bad operand types for binary operators in Java”, and how to resolve the problem.
How many types of Binary Operators are Used in Java?
There are 8 names you may have already heard: Arithmetic Operators, Assignment Operators, Logical Operators, Bitwise Operators, Relational Operators, Shift Operators, Unary Operators, Ternary Operators.
Arithmetic operators
This function performs arithmetic operations on variables and data.
Example
class Main { public static void m // declare variablesain(String[] args) { int a = 12, b = 5; // addition operator System.out.println("a + b = " + (a + b)); // subtraction operator System.out.println("a - b = " + (a - b)); // multiplication operator System.out.println("a * b = " + (a * b)); // division operator System.out.println("a / b = " + (a / b)); // modulo operator System.out.println("a % b = " + (a % b)); } }
Assignment operators
This function help assign values to variables.
Example
int age; age = 5;
Relational operators
This function help check the relationship between two operands.
Example:
// check if a is less than b a < b;
Logical operators
This operators are used to check whether an identity is true or false. They are used in decision making.
class Main { public static void main(String[] args) { // && operator System.out.println((5 > 3) && (8 > 5)); // true System.out.println((5 > 3) && (8 < 5)); // false // || operator System.out.println((5 < 3) || (8 > 5)); // true System.out.println((5 > 3) || (8 < 5)); // true System.out.println((5 < 3) || (8 < 5)); // false // ! operator System.out.println(!(5 == 3)); // true System.out.println(!(5 > 3)); // false } }
Unary operators
This are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1. That is, ++5 will return 6.
Shift Operators
This operators are used for shifting purposes. It used to shift bits of a number either left to right or vice versa. In Java, there are two types of shift operators
- Left Shift (<<)
- Right Shift (>>)
Ternary Operators
Every Ternary operator has one condition and two outputs. If the condition is True, the first output is returned, if not then the second output is returned.
The basic instruct of a Ternary operator is:
(condition) ? (Output 1) : (Output 2)
Example:
class Program { public static void main (String agmt[]) { //Using Assignment Operator int n1 = 25; int n2 = 58; int n3 = 76; //Using Relational Operator if(n2 > n1) System.out.println(“n2 is greater than n1”); //Using Arithmetic Operator n3 = n1 + n2; //Using Logical Operator if (n3>n1 && n3>n2) System.out.println(“n3 is the greatest number”); } }
Output
n2 is greater than n1
n3 is the greatest number
Bitwise Operators
There are six bitwise Operators: &, |, ^, ~, <<, >>
num1 = 11; /* equal to 00001011*/ num2 = 22; /* equal to 00010110 */
- & (bitwise AND) Takes two numbers as operands and performs AND on each bit of these two numbers. The result of AND will be just 1, both bits are 1.
- | (bitwise OR) Takes two numbers as operands and performs an OR on each bit of these two numbers. The result of OR will be only 1, both bits are 1.
- ^ (bitwise XOR) Takes two numbers as operands and performs XOR on each bit of these two numbers. If the two bits are different, the result of XOR is 1.
- << (left shift) Takes two numbers, shifts the bits of the first operand to the left, the second operand determines the number of spaces to shift.
- >> (right shift) Takes two numbers, shifts the bits of the first operand to the right, the second operand determines the number of spaces to shift.
~ (bitwise NOT) Takes a number and inverses all bits of that number.
Example
#include <iostream> using namespace std; int main(){ int num1 = 11; /* 11 = 00001011 */ int num2 = 22; /* 22 = 00010110 */ int result = 0; result = num1 & num2; cout<<"num1 & num2: "<<result<<endl; result = num1 | num2; cout<<"num1 | num2: "<<result<<endl; result = num1 ^ num2; cout<<"num1 ^ num2: "<<result<<endl; result = ~num1; cout<<"~num1: "<<result<<endl; result = num1 << 2; cout<<"num1 << 2: "<<result<<endl; result = num1 >> 2; cout<<"num1 >> 2: "<<result; return 0; }
Output:
num1 & num2: 2 num1 | num2: 31 num1 ^ num2: 29 ~num1: -12 num1 << 2: 44 num1 >> 2: 2
Binary Operators in Java
For binary operator & in Java
Let’s first understand the most common error where bad operator error can occur in Java code. Look at an example code below:
public class MyClass { public static void main(String args[]) { int x = 43; if( x & 21 == 1){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
MyClass.java:4: error: bad operand types for binary operator '&' if( x & 21 == 1){ ^ first type: int second type: boolean 1 error
This error occurs when the priority rate of the & operator is less than the == operator. This leads to the 21==1 evaluation, which gives us a boolean value.
Now, please notice the & has one integer operand and one boolean. The cause of this issue is that both of the operators are now different. It’s natural for problems to happen.
In here, we can use parenthesis to so that x & 21 needs to be evaluated first if you would like to resolve this error. Look at the modified code below:
public class MyClass { public static void main(String args[]) { int x = 43; if( (x & 21) == 1){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
if block executing
For binary operator && in Java
The second error we would like to mention is with && operator. While the implementary with logical && operator, maybe you met the bad operand type error as the below example code:
public class MyClass { public static void main(String args[]) { int x = 43; if((x > 10) && (x*5)){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
MyClass.java:4: error: bad operand types for binary operator '&&' if((x > 10) && (x*5)){ ^ first type: boolean second type: int 1 error
The definition x * 5 gives an integer value that is reason when the problem happen.
The best solution is modify this code such that x * 5==21 to return a boolean value. You can refer as below:
public class MyClass { public static void main(String args[]) { int x = 43; if((x > 10) && (x*5 == 21)){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
else block executing
For binary operator == in Java
This situation can happen when working with the == equals operator. It can give an error if both the operands passed are of different types. Like below example:
public class MyClass { public static void main(String args[]) { int x = 43; String y = "43"; if(x == y){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
MyClass.java:5: error: bad operand types for binary operator '==' if(x == y){ ^ first type: int second type: String 1 error
The operands of the == equals operator belong to different types, a string, and an integer.
This time, we will have to convert one of them to get the same data types. The solution is we can convert integers into strings, the comparison will occur in lexicological order.
So, we will convert string to int. Look at the code below:
public class MyClass { public static void main(String args[]) { int x = 43; String y = "43"; if(x == Integer.parseInt(y)){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
if block executing
Bad Operand Types for the <= Operator in Java
And the last one, like the previous case example, the <= (less than equals to) operator can also give an error when both the operands are of different types. For instant:
public class MyClass { public static void main(String args[]) { int x = 43; String y = "43"; if(x <= y){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
MyClass.java:5: error: bad operand types for binary operator '<=' if(x <= y){ ^ first type: int second type: String 1 error
The solution that you can try is to convert one of them to get the same data types.
public class MyClass { public static void main(String args[]) { int x = 43; String y = "43"; if(x <= Integer.parseInt(y)){ System.out.println("if block executing"); } else{ System.out.println("else block executing"); } } }
Output:
if block executing
To sum up
All programming languages have their own advantages and disadvantages. And the issue that comes out is very normal. We hope that the above information are useful to you. Or after reading all but you still need support from our side, then just need a click and contact us. With our 10-year experience, not only give the website or mobile app development solutions but also UI/UX design services Onext Digital will not disappointed the highest demand customer. Cooperating with you is our honor.
>> Read more
Top 35 Well-Known PWA Examples In 2023