How to Combine Conditions with AND (&&), OR (||) and NOT (!) in Java
Java combines true-or-false conditions with three logical operators: a && b is true when both sides are true, a || b when at least one is, and !a turns true into false and false into true. && and || skip their right side when the left side already decides the answer, which lets a check such as nickname != null protect the code after it.
Syntax
<a> && <b>
<a> || <b>
!<a>
| Part | What it does |
|---|---|
<a> && <b> | AND: true only when both <a> and <b> are true. If <a> is false, <b> isn't evaluated. |
<a> || <b> | OR: true when <a>, <b> or both are true. If <a> is true, <b> isn't evaluated. |
!<a> | NOT: true when <a> is false, and false when it's true. |
<a>, <b> | Expressions of type boolean, such as age >= 65 or a boolean variable. |
The Java Tutorials, Equality, Relational, and Conditional Operators: "The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed." The page on Assignment, Arithmetic, and Unary Operators lists ! as the "Logical complement operator; inverts the value of a boolean".
The Example
The example checks a user who is signed in, isn't banned and is 70. It prints one &&, one || and one !, then uses && to check that a nickname isn't null before it calls a method on it. A changed version of the program then uses & instead of &&, which always evaluates both sides.
Prerequisites
- A JDK 11 or later, which can run a single
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

public class Main {
public static void main(String[] args) {
boolean signedIn = true;
boolean banned = false;
int age = 70;
// && is true only when both sides are true.
System.out.println("can comment: " + (signedIn && !banned));
// || is true when at least one side is true.
System.out.println("pays the reduced price: " + (age < 12 || age >= 65));
// ! turns true into false, and false into true.
System.out.println("must sign in: " + !signedIn);
String nickname = null;
// && skips its right side when the left side is false, so isEmpty() never runs on null.
System.out.println("has a nickname: " + (nickname != null && !nickname.isEmpty()));
}
}
Step 2: Run It
java Main.java
can comment: true
pays the reduced price: true
must sign in: false
has a nickname: false
java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.
Step 3: Use & Instead of &&
Replace Main.java with a version that checks the nickname with &, which evaluates both sides every time:
public class Main {
public static void main(String[] args) {
String nickname = null;
// & always runs both sides, so isEmpty() runs on null.
System.out.println("has a nickname: " + (nickname != null & !nickname.isEmpty()));
}
}
java Main.java
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "<local1>" is null
at Main.main(Main.java:6)

What Happened
signedIn && !bannedistrue.!bannedturnedfalseintotrue, so both sides were true.age < 12 || age >= 65istrue. The left side was false, but the right side was true for an age of 70.!signedInisfalse, becausesignedInistrue.nickname != null && !nickname.isEmpty()isfalse, with no error. The left side was false, so&&never evaluatednickname.isEmpty().- With
&, the program stopped.&gives the same answer as&&, but it evaluatednickname.isEmpty()althoughnicknamewasnull, and calling a method onnullthrows aNullPointerException.
The Java Language Specification says the same about &&: it "is like &", "but evaluates its right-hand operand only if the value of its left-hand operand is true."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using a number with &&, as in age && signedIn | error: bad operand types for binary operator '&&', because &&, || and ! take only boolean values | Compare the number first, as in age >= 65 |
Writing !age >= 65 to mean "not 65 or over" | error: bad operand type int for unary operator '!', because ! applies to age first | Add parentheses: !(age >= 65) |
Using & or | where the right side depends on the left | Both sides always run, so nickname.isEmpty() runs on null | Use && and || |
Mixing && and || without parentheses | a || b && c means a || (b && c), which can differ from what you meant | Add parentheses to say which pair goes first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
error: bad operand types for binary operator '&&', with first type: int | One side of && isn't a boolean | Turn it into a comparison |
error: bad operand type int for unary operator '!' | ! stands before a number, or before a comparison without parentheses | Put the comparison in parentheses after ! |
NullPointerException: Cannot invoke "String.isEmpty()" because "<local1>" is null | A method was called on a null variable | Check for null first and join the two with && |
FAQ
What's the difference between & and && in Java?
On boolean values, both give true only when both sides are true. && skips its right side when the left side is false, and & always evaluates both. || and | differ in the same way.
Why does the error say <local1> instead of nickname?
java Main.java compiles the file without the names of local variables. JEP 358, which added these detailed messages, says that names are printed "if debug information is included in the class file (via javac -g)". Without that information, the message numbers the variable instead.
Which runs first, && or ||?
&& binds tighter than ||, so a || b && c means a || (b && c). With a true and c false, a || b && c printed true, and (a || b) && c printed false.
Conclusion
&& needs both sides to be true, || needs one, and ! flips a condition. Because && and || stop as soon as the answer is known, a null check on the left keeps the method call on the right from running on null. & and | don't stop, so use them only when both sides must run.
Additional Resources
- Equality, Relational, and Conditional Operators and Assignment, Arithmetic, and Unary Operators, in The Java Tutorials.
- Conditional-And Operator and Conditional-Or Operator, in the Java Language Specification.
- JEP 358: Helpful NullPointerExceptions.
