Coding/Learn/java/logical operators
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnLogical Operators (&&, ||, !)
12 / 31 0%
Lesson 12 of 31
6 mins read

Logical Operators (&&, ||, !)

PreviousNext Lesson
In Plain English

Logical operators let you build complex conditional decisions by combining multiple boolean expressions. Java features short-circuit evaluation for blazing performance and safe object checks.

The Mental Model

"Think of getting into a movie theater: you need a ticket AND (&&) an ID. For popcorn: you can pay with cash OR (||) credit card. If you do NOT (!) have a ticket, you cannot enter."

Deep Dive: How It Works

`&&` (Logical AND): true only if BOTH operands are true.

`||` (Logical OR): true if AT LEAST ONE operand is true.

`!` (Logical NOT): inverts the boolean value (true becomes false, false becomes true).

Short-Circuit Evaluation: For `&&`, if the first condition is false, Java stops immediately and evaluates to false. For `||`, if the first condition is true, Java skips the rest.

Syntax Blueprint

boolean allowAccess = (hasKeycard && isWorkHour) || isAdmin;
boolean isLocked = !isOpen;

Use parentheses to explicitly control evaluation precedence.

PointCore Rules to Remember

Point star
Short-Circuit AND (&&): Stops evaluating as soon as one condition is false.
Point star
Short-Circuit OR (||): Stops evaluating as soon as one condition is true.
Point star
NOT Operator (!): Flips truth value: !true is false.

Common Beginner Traps & How to Fix Them

Mistake:Using single & or | instead of && or ||

Why it happens: Single & and | are bitwise operators that do NOT short-circuit.

How to fix: Always use double && and || for standard boolean logic.

Pro Tip: Short-circuiting lets you safely guard against null references: if (user != null && user.isActive()).

Live Interactive Example

Hit Run Code to see it live
Logical Access Verification
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: All three AND conditions are satisfied for car rental. isUnderage is false because age is 21.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Check discount eligibility

  • PointA customer qualifies for a discount if isMember is true OR totalSpend is greater than 100.
  • PointGiven isMember = false and totalSpend = 120, check eligibility and print "Eligible: " + isEligible.
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates