๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_operators_logical.asp
Java Logical Operators
Booleans Real-Life Example Code Challenge Java If...Else ยท if else else if Short Hand If...Else Nested If Logical Operators Real-Life Examples Code Challenge Java Switch
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_operators.asp
Java Operators
Booleans Real-Life Example Code Challenge Java If...Else ยท if else else if Short Hand If...Else Nested If Logical Operators Real-Life Examples Code Challenge Java Switch
Discussions

java - Differences in boolean operators: & vs && and | vs || - Stack Overflow
Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different behaviors of the operator based on its inputs. Indeed when both inputs are boolean, the operators are considered the Boolean Logical Operators and behave similar to ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
arraylist - How to apply a logical operator to all elements in Java - Stack Overflow
I need to apply the && (logical and) operator to all elements in the list. The problem is: In my current solution, I need to initialize the rowResult variable, and I think the result may be... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How the logical operators works in java? - Stack Overflow
I executed the below java code in IDE. the first sysout prints false. where as second sysout prints true. Some websites says because of operator precedence this occurs. System.out.println(false &am... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Are the &, |, ^ bitwise operators or logical operators? - Stack Overflow
Firstly I learnt that &, |, ^ are the bitwise operators, and now somebody mentioned them as logical operators with &&, ||, I am completely confused - the same operator has two names? Th... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-logical-operators-with-examples
Java Logical Operators with Examples - GeeksforGeeks
April 16, 2025 - Java Collection ยท Last Updated : 16 Apr, 2025 ยท Logical operators are used to perform logical "AND", "OR", and "NOT" operations, i.e., the functions similar to AND gate and OR gate in digital electronics.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ operators-in-java
Java Operators - GeeksforGeeks
Logical Operators are used to perform "logical AND" and "logical OR" operations, similar to AND gate and OR gate in digital electronics. They have a short-circuiting effect, meaning the second condition is not evaluated if the first is false. Java ยท
Published ย  November 12, 2025
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ tutorial โ€บ java โ€บ nutsandbolts โ€บ operators.html
Operators (The Javaโ„ข Tutorials > Learning the Java Language > Language Basics)
Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right ...
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ java_logical_operators_examples.htm
Java - Logical Operators
Java logical operators are used to perform logical operations on boolean values. These operators are commonly used in decision-making statements such as conditions and loops to control program flow.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Java LOGICAL OPERATORS are easy! โ• - YouTube
#java #javatutorial #javacourse In Java, logical operators are used to combine or negate Boolean expressions, allowing conditions to be evaluated together.(&...
Published ย  December 5, 2024
Find elsewhere
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ java_basic_operators.htm
Java Operators
Logical operators are used to perform logical operations on boolean values.
๐ŸŒ
Medium
java-jedi.medium.com โ€บ logical-operator-tricks-you-might-not-know-98ce621f78bc
Logical Operator Tricks you might not know. | by Java Jedi | Medium
October 9, 2025 - In conclusion, logical operators like &&, ||, &, and | are foundational to Java programming.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ java-operator-and-or-logical-operators
Java Operator โ€“ &, && (AND) || (OR) Logical Operators
February 8, 2022 - In this article, we will be talking about the bitwise AND operator, and the AND (&&) and OR (||) logical operators.
๐ŸŒ
Trinket
books.trinket.io โ€บ thinkjava โ€บ chapter5.html
Conditionals and logic | Think Java | Trinket
Weโ€™ll explain what they do later; in the meantime, donโ€™t use them with strings. Instead, you should use the equals method: The result of fruit1.equals(fruit2) is the boolean value false. Java has three logical operators: &&, ||, and !, which respectively stand for and, or, and not.
Top answer
1 of 11
158

Those are the bitwise AND and bitwise OR operators.

int a = 6; // 110
int b = 4; // 100

// Bitwise AND    

int c = a & b;
//   110
// & 100
// -----
//   100

// Bitwise OR

int d = a | b;
//   110
// | 100
// -----
//   110

System.out.println(c); // 4
System.out.println(d); // 6

Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different behaviors of the operator based on its inputs.

Indeed when both inputs are boolean, the operators are considered the Boolean Logical Operators and behave similar to the Conditional-And (&&) and Conditional-Or (||) operators except for the fact that they don't short-circuit so while the following is safe:

if((a != null) && (a.something == 3)){
}

This is not:

if((a != null) & (a.something == 3)){
}

"Short-circuiting" means the operator does not necessarily examine all conditions. In the above examples, && will examine the second condition only when a is not null (otherwise the whole statement will return false, and it would be moot to examine following conditions anyway), so the statement of a.something will not raise an exception, or is considered "safe."

The & operator always examines every condition in the clause, so in the examples above, a.something may be evaluated when a is in fact a null value, raising an exception.

2 of 11
117

I think you're talking about the logical meaning of both operators, here you have a table-resume:

boolean a, b;

Operation     Meaning                       Note
---------     -------                       ----
   a && b     logical AND                    short-circuiting
   a || b     logical OR                     short-circuiting
   a &  b     boolean logical AND            not short-circuiting
   a |  b     boolean logical OR             not short-circuiting
   a ^  b     boolean logical exclusive OR
  !a          logical NOT

short-circuiting        (x != 0) && (1/x > 1)   SAFE
not short-circuiting    (x != 0) &  (1/x > 1)   NOT SAFE

Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.

Not Safe means the operator always examines every condition in the clause, so in the examples above, 1/x may be evaluated when the x is, in fact, a 0 value, raising an exception.

๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ operators
Java Operators: Arithmetic, Relational, Logical and more
Note: Relational operators are used in decision making and loops. Logical operators are used to check whether an expression is true or false.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 78012112 โ€บ how-the-logical-operators-works-in-java
How the logical operators works in java? - Stack Overflow
Because that's how false && true | true is resolved. See docs.oracle.com/javase/tutorial/java/nutsandbolts/โ€ฆ ... The code is not executed left to right: it is executed in the order of the operator precedence table docs.oracle.com/javase/tutorial/java/nutsandbolts/โ€ฆ
๐ŸŒ
Freejavaguide
freejavaguide.com โ€บ boolean_operators.htm
Boolean operators - Java tutorial | freejavaguide.com
It's very easy to learn java programming skills, and in these parts, you'll learn how to write, compile, and run Java applications. Before you can develop corejava applications, you'll need to download the Java Development Kit (JDK). ... The Boolean logical operators are : | , & , ^ , !
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ java operators
Java Operators | Baeldung
January 8, 2024 - We have two logical operators in Java: the logical AND and OR operators.
Top answer
1 of 4
22

The Java operators &, | and ^ are EITHER bitwise operators OR logical operators ... depending on the types of the operands. If the operands are integers, the operators are bitwise. If they are booleans, then the operators are logical.

And this is not just me saying this. The JLS describes these operators this way too; see JLS 15.22.

(This is just like + meaning EITHER addition OR string concatenation ... depending on the types of the operands. Or just like a "rose" meaning either a flower or a shower attachment. Or "cat" meaning either a furry animal or a UNIX command. Words mean different things in different contexts. And this is true for the symbols used in programming languages too.)


There are already logical operators &&, ||, why use &, |, ^?

In the case of the first two, it is because the operators have different semantics with regards to when / whether the operands get evaluated. The two different semantics are needed in different situations; e.g.

Copy    boolean res = str != null && str.isEmpty();

versus

Copy    boolean res = foo() & bar();  // ... if I >>need<< to call both methods.

The ^ operator has no short-circuit equivalent because it simply doesn't make sense to have one.

2 of 4
3

Having a language reference is one thing, interpreting it correctly is another.

We need to interpret things correctly.

Even if Java documented that & is both bitwise and logical, we could make an argument that & really didn't lost its logical-operator-ness mojo since time immemorial, since C. That is, & is first and foremost, an inherently logical operator(albeit a non-short-circuited one at that)

& parses lexically+logically as logical operation.

To prove the point, both of these lines behaves the same, eversince C and upto now(Java, C#, PHP, etc)

Copyif (a == 1 && b)

if (a == 1 & b)

That is, the compiler will interpret those as these:

Copyif ( (a == 1) && (b) )

if ( (a == 1) & (b) )

And even if both variables a and b are both integers. This...

Copyif (a == 1 & b)

... will still be interpereted as:

Copyif ( (a == 1) & (b) )

Hence, this will yield a compilation error on languages which doesn't facilitate integer/boolean duality, e.g. Java and C#:

Copyif (a == 1 & b)

In fact, on the compilation error above, we could even make an argument that & didn't lost its logical(non-short-circuit) operation mojo, and we can conclude that Java continues the tradition of C making the & still a logical operation. Consequently, we could say it's the other way around, i.e. the & can be repurposed as bitwise operation (by applying parenthesis):

Copyif ( a == (1 & b) )

So there we are, in another parallel universe, someone could ask, how to make the & expression become a bitmask operation.

How to make the following compile, I read in JLS that & is a bitwise operation. Both a and b are integers, but it eludes me why the following bitwise operation is a compilation error in Java:

if (a == 1 & b)

Or this kind of question:

Why the following didn't compile, I read in JLS that & is a bitwise operation when both its operands are integers. Both a and b are integers, but it eludes me why the following bitwise operation is a compilation error in Java:

if (a == 1 & b)

In fact, I would not be surprised if there's already an existing stackoverflow questions similar to above questions that asked how to do that masking idiom in Java.

To make that logical operation interpretation by the language become bitwise, we have to do this (on all languages, C, Java, C#, PHP, etc):

Copyif ( a == (1 & b) )

So to answer the question, it's not because JLS defined things such way, it's because Java(and other languages inspired by C)'s & operator is for all intents and purposes is still a logical operator, it retained C's syntax and semantics. It's the way it is since C, since time immemorial, since before I was even born.

Things just don't happen by chance, JLS 15.22 didn't happen by chance, there's a deep history around it.

In another parallel universe, where && was not introduced to the language, we will still be using & for logical operations, one might even ask a question today:

Is it true, we can use the logical operator & for bitwise operation?

& doesn't care if its operands are integers or not, booleans or not. It's still a logical operator, a non-short-circuited one. And in fact, the only way to force it to become a bitwise operator in Java(and even in C) is to put parenthesis around it. i.e.

Copyif ( a == (1 & b) )

Think about it, if && was not introduced to C language(and any language who copied its syntax and semantics), anyone could be asking now:

how to use & for bitwise operations?

To sum it up, first and foremost Java & is inherently a logical operator(a non-short-circuited one), it doesn't care about its operands, it will do its business as usual(applying logical operation) even if both operands are integers(e.g. masking idiom). You can only force it to become bitwise operation by applying parenthesis. Java continues the C tradition

If Java's & really is a bitwise operation if its operands(integer 1 and integer variable b on example code below) are both integers, this should compile:

Copy int b = 7;
 int a = 1;

 if (a == 1 & b) ...
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ java logical operators
Java Logical Operators: Explained & Examples | StudySmarter
Java logical operators are fundamental components in programming that allow developers to perform logical operations on boolean expressions. The three primary logical operators in Java are: `&&` (AND), `||` (OR), and `!` (NOT), each serving to control the flow of decision-making processes by ...