Чем инициализируется по умолчанию локальная переменная ссылочного типа java
Перейти к содержимому

Чем инициализируется по умолчанию локальная переменная ссылочного типа java

  • автор:

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:

&& 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
1 Operator It is a bitwise AND operator. It is a logical AND operator.
2 Evaluation It evaluates both the left and right side of the given expression. It only evaluates the left sides of the given expression.
3 Operates on It operates on Boolean data types as well as on bits. It operates only on Boolean datatype.
4 Uses Used to check logical condition and also used to mask off certain bits such as parity bits. Used only to check the logical conditions.
5 Example z = x & y if (y > 1 && y > x)

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



true false


Logical AND (&&)

The logical AND ( && ) (logical conjunction) operator for a set of boolean operands will be true if and only if all the operands are true . Otherwise it will be false .

More generally, the operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.

Try it



If a value can be converted to true , the value is so-called truthy. If a value can be converted to false , the value is so-called falsy.

Examples of expressions that can be converted to false are:

  • false ;
  • null ;
  • NaN ;
  • 0 ;
  • empty string ( «» or » or « );
  • undefined .

The AND operator preserves non-Boolean values and returns them as they are:

= "" && "foo"; // result is assigned "" (empty string) result = 2 && 0; // result is assigned 0 result = "foo" && 4; // result is assigned 4 

Even though the && operator can be used with non-Boolean operands, it is still considered a boolean operator since its return value can always be converted to a boolean primitive. To explicitly convert its return value (or any expression in general) to the corresponding boolean value, use a double NOT operator or the Boolean constructor.

Short-circuit evaluation

The logical AND expression is a short-circuit operator. As each operand is converted to a boolean, if the result of one conversion is found to be false , the AND operator stops and returns the original value of that falsy operand; it does not evaluate any of the remaining operands.

Consider the pseudocode below.

(some falsy expression) && expr

The expr part is never evaluated because the first operand (some falsy expression) is evaluated as falsy. If expr is a function, the function is never called. See the example below:

function A()  console.log("called A"); return false; > function B()  console.log("called B"); return true; > console.log(A() && B()); // Logs "called A" to the console due to the call for function A, // && evaluates to false (function A returns false), then false is logged to the console; // the AND operator short-circuits here and ignores function B 

Operator precedence

The AND operator has a higher precedence than the OR operator, meaning the && operator is executed before the || operator (see operator precedence).

true || false && false; // true true && (false || false); // false (2 === 3) || (4  0) && (1 === 1); // false 


Using AND

The following code shows examples of the && (logical AND) operator.

= true && true; // t && t returns true a2 = true && false; // t && f returns false a3 = false && true; // f && t returns false a4 = false && 3 === 4; // f && f returns false a5 = "Cat" && "Dog"; // t && t returns "Dog" a6 = false && "Cat"; // f && t returns false a7 = "Cat" && false; // t && f returns false a8 = "" && false; // f && f returns "" a9 = false && ""; // f && f returns false 

Conversion rules for booleans

Converting AND to OR

The following operation involving booleans:

&& bCondition2 

is always equal to:

!(!bCondition1 || !bCondition2) 
Converting OR to AND

The following operation involving booleans:

|| bCondition2 

is always equal to:

!(!bCondition1 && !bCondition2) 

Removing nested parentheses

As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression provided that certain rules are followed.

The following composite operation involving booleans:

|| (bCondition2 && bCondition3) 

is always equal to:

|| bCondition2 && bCondition3 


ECMAScript Language Specification
# prod-LogicalANDExpression

Browser compatibility

BCD tables only load in the browser

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *