Logical operators in Java
Logical operators in Java are used to combine multiple boolean expressions or conditions, and they are essential for writing decision-making logic like if statements and loops.
Logical AND (&&)
Returns true only if both conditions on either side are true. It uses short-circuit evaluation, meaning the second condition isn't checked if the first is already false.
int age = 25;
boolean hasID = true;
if (age >= 18 && hasID) {
System.out.println("Entry allowed");
}
Logical OR (||)
Returns true if at least one of the two conditions is true. It also short-circuits — if the first condition is true, the second is never evaluated.
boolean isWeekend = true;
boolean isHoliday = false;
if (isWeekend || isHoliday) {
System.out.println("No work today");
}
Logical NOT (!)
Reverses the value of a boolean expression — true becomes false and vice versa.
boolean isLoggedIn = false;
if (!isLoggedIn) {
System.out.println("Please log in");
}
Truth Table
true && true→truetrue && false→falsefalse || true→truefalse || false→false!true→false
Short-Circuit Evaluation
Java also provides non-short-circuit versions, & and |, which always evaluate both sides. These are rarely used for boolean logic but matter to know, since && and || skip unnecessary evaluations for better performance and to avoid errors like calling a method on a null object.
obj != null && obj.isValid() safely.Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
Operator Precedence When Conditions Are Mixed
Once you combine more than two conditions, Java decides the order of evaluation using precedence. The NOT operator ! binds tightest, then &&, and || comes last. Relational operators such as >, <= and == are evaluated before any of the logical ones.
This means a || b && c is read by the compiler as a || (b && c), not (a || b) && c. The two versions can give different answers, so add parentheses whenever a reader might have to stop and think.
boolean isAdmin = false;
boolean isOwner = true;
boolean isLocked = true;
// Read as: isAdmin || (isOwner && !isLocked)
boolean canEdit = isAdmin || isOwner && !isLocked; // false
// Clearer, and the intent is obvious
boolean canEditClear = isAdmin || (isOwner && !isLocked);
Worked Example: Validating a Form Field
Suppose a sign-up form accepts a username only if it is not null, has between 4 and 15 characters, and does not start with a digit. Each rule is a separate boolean check, and logical operators glue them into one decision.
static boolean isValidUsername(String name) {
return name != null
&& name.length() >= 4
&& name.length() <= 15
&& !Character.isDigit(name.charAt(0));
}
System.out.println(isValidUsername("ravi_k")); // true
System.out.println(isValidUsername("7star")); // false
System.out.println(isValidUsername(null)); // false, no exception
Step by step, the method first checks for null. If name is null, && stops right there, so name.length() never runs and no NullPointerException is thrown. Only when the earlier checks pass does Java look at the first character. Ordering the cheap, protective checks first is the habit to build.
The XOR operator and De Morgan's laws
Java also has ^, the exclusive OR. With booleans it returns true when exactly one side is true, which is handy for toggles or "one or the other, not both" rules. It always evaluates both sides.
De Morgan's laws help you rewrite negated conditions so they read naturally:
!(a && b)is the same as!a || !b!(a || b)is the same as!a && !b
For example, !(isWeekend || isHoliday) becomes !isWeekend && !isHoliday, which many people find easier to read as "a normal working day".
Mistakes Beginners Make With Boolean Logic
- Using
&instead of&&. The single ampersand evaluates both sides, so a null check on the left no longer protects the method call on the right. - Putting side effects in the right operand. Code like
if (ok && count++ > 0)incrementscountonly sometimes, which leads to confusing bugs. - Writing
if (flag == true). It works, butif (flag)is shorter. Worse, a typo such asif (flag = true)compiles and always passes. - Chaining a range check wrongly.
10 < x < 20does not compile in Java; writex > 10 && x < 20. - Using
&&on integers. Logical operators work only on booleans. Bit manipulation on numbers needs the bitwise&,|and^.
Quick checklist: place null and bounds checks first, wrap mixed &&/|| groups in parentheses, keep operands free of side effects, and move a long condition into a well-named boolean method so the if line explains itself.