Difference Between & and && in JavaIn 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. & OperatorThe 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 && OperatorThe 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. 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 Next TopicJumping Number in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India