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.

Difference Between & and && in Java

& Operator

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

&& Operator

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.

S.N.Basis& Operator&& Operator
1OperatorIt is a bitwise AND operator.It is a logical AND operator.
2EvaluationIt evaluates both the left and right side of the given expression.It only evaluates the left sides of the given expression.
3Operates onIt operates on Boolean data types as well as on bits.It operates only on Boolean datatype.
4UsesUsed to check logical condition and also used to mask off certain bits such as parity bits.Used only to check the logical conditions.
5Examplez = x & yif (y > 1 && y > x)

Let's understand bitwise and logical and operator through a Java program.

LogicalAndExample.java

Output:

true
false

BitwiseAndExample.java

Output:

a&b=1

Let's create another Java program and use Logical and Bitwise AND operators simultaneously.

JavaOperators.java

Output:

8
true