Difference Between & and && in Java
In this section, we will discuss the two most important operators & and && in Java and also see the key differences between logical and bitwise operators along with its uses.
The single AND operator (&) is known as the Bitwise AND operator. It operates on a single bit. It takes two operands. A bit in the result is 1 if and only if both of the corresponding bits in the operands are 1. The result of the operator may be any number. For example:
a = 01100010
b = 10111010
a&b = 00100010
The double AND operators (&&) are known as logical AND operators. It is usually used in loops and conditional statements. It is usually used in Boolean expressions. The result of && is always 0 or 1.
Difference Between & and &&
The key difference between && and & operators is that && supports short-circuit evaluations while & operator does not.
Another difference is that && will evaluate the expression exp1, and immediately return a false value if exp1 is false. While & operator always evaluates both expressions (exp1 and exp2) before retiring an answer.
Let's understand bitwise and logical and operator through a Java program.
Let's create another Java program and use Logical and Bitwise AND operators simultaneously.