Difference Between & and && in Java

Codenaive
2 min readMay 1, 2021

--

Bitwise and Logical AND Operators

A single AND operator (&) represents the bitwise AND operator in java. While the double AND operators (&&) are known as Logical AND operators in java.

Bitwise AND Operator

A bitwise operator produces a 1 bit if both the operands are also 1. A zero(0) is produced in all other left cases. For example:

10010001  31  &	00110011  51 	--------------	00010001  19

Logical AND Operator

A bitwise Logical AND operators are used to control the loops and conditional statements, it always return true if both conditions are true, it usually returns either true or false. For example:

Table of Contents

  1. Bitwise and Logical AND Operators
  2. Bitwise AND Operator
  3. Logical AND Operator
  4. Difference Between & and &&
  5. Bitwise and Logical AND Operators Program
  6. Program Example Of Bitwise AND Operator
  7. Output of Bitwise AND Operator
  8. Program Example Of Logical AND Operator
  9. Output of Logical AND Operator
  10. Combined program of Logical AND Operator and Bitwise AND operator
  11. Output of Logical AND Operator

Difference Between & and &&

A Logical AND evalutes expression1, if it true then it will check the next condition otherwise immediately return a false. After expression check, if all expression is true then Logical AND will return true. The Key Difference between & and && is that short-circuit operation only supported by the Logical AND Operator, Bitwise doesn’t support it. evaluationsSNBasis& Operator&& Operators1Operator TypeIt is a Bitwise AND operator.It is a Logical AND Operator.2RepresentationOnly with Single AND (&).Represent as double AND (&&)3EvaluationIt compares two bits at a time (left and right on both sides) and returns the approperiate result based on condition.It only evaluates the next condition, if the left side condition is true. If all condition satisfied then it returns the true otherwise false.4OperationsPerform bit shift operations.Evaluate the expression then perform bit shift operations.4Short-CircuitIt doesn’t support.A Logical AND support short-circuit.5UsesTo operate the logical and bitmask operations.To evaluate the logical conditions.6Exampleresult = a & b;if( x < y && y < z)

Bitwise and Logical AND Operators Program

The following example illustrates the uses of both bitwise and logical AND operators.

Program Example Of Bitwise AND Operator

In the following example, we use the Bitwise AND operator to perform some bit operation in java.

public class DemoBitwiseAND{     public static void main(String []args){
int x=31; // create a constant x = 31
int y=51; // create a constant y = 51

int result = x & y; // perform bitwise operation on x and y constants

System.out.println("Bitwise & operation of x and y is "+result); // display result


}
}

Output of Bitwise AND Operator

Bitwise & operation of x and y is 19

READ MORE : https://www.codenaive.com/edu/java/bitwise-and-vs-logical-and-in-java

--

--