Skip to main content

How to Combine Conditions with AND (&&), OR (||) and NOT (!) in Java

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<a> && <b>
<a> || <b>
!<a>
PartWhat 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 .java file directly. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which sets signedIn to true, banned to false and age to 70, prints signedIn AND NOT banned, age under 12 OR at least 65, and NOT signedIn, then prints whether a null nickname is not null AND not empty.

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​

Terminal
java Main.java
Output
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:

Main.java
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()));
}
}
Terminal
java Main.java
Output
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.isEmpty()" because "<local1>" is null
at Main.main(Main.java:6)

A zsh terminal where java Main.java prints can comment: true, pays the reduced price: true, must sign in: false and has a nickname: false, then, for the changed file, stops with a NullPointerException because String.isEmpty() was called on null.

What Happened​

  • signedIn && !banned is true. !banned turned false into true, so both sides were true.
  • age < 12 || age >= 65 is true. The left side was false, but the right side was true for an age of 70.
  • !signedIn is false, because signedIn is true.
  • nickname != null && !nickname.isEmpty() is false, with no error. The left side was false, so && never evaluated nickname.isEmpty().
  • With &, the program stopped. & gives the same answer as &&, but it evaluated nickname.isEmpty() although nickname was null, and calling a method on null throws a NullPointerException.

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​

MistakeWhat happensDo this instead
Using a number with &&, as in age && signedInerror: bad operand types for binary operator '&&', because &&, || and ! take only boolean valuesCompare 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 firstAdd parentheses: !(age >= 65)
Using & or | where the right side depends on the leftBoth sides always run, so nickname.isEmpty() runs on nullUse && and ||
Mixing && and || without parenthesesa || b && c means a || (b && c), which can differ from what you meantAdd parentheses to say which pair goes first

Troubleshooting​

SymptomLikely causeFix
error: bad operand types for binary operator '&&', with first type: intOne side of && isn't a booleanTurn it into a comparison
error: bad operand type int for unary operator '!'! stands before a number, or before a comparison without parenthesesPut the comparison in parentheses after !
NullPointerException: Cannot invoke "String.isEmpty()" because "<local1>" is nullA method was called on a null variableCheck 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​