Logical Operators Part 4
Part of the Fundamentals section of Coddy's Java journey — lesson 26 of 73.
De Morgan's Law is a rule that helps us simplify expressions with NOT in them. When working with logical expressions, sometimes we need to simplify or rearrange them.
When you have not in front of two conditions joined by and, you can split it into two separate parts. The and becomes or, and each part gets its own not:
!(A && B) is the same as (!A) || (!B)
For example:
// Let's check if a number is NOT (between 1 and 10)
int number = 15;
// These two expressions are equivalent:
boolean result1 = !(number >= 1 && number <= 10);
boolean result2 = !(number >= 1) || !(number <= 10);
System.out.println(result1); // true
System.out.println(result2); // trueThe opposite is also correct: !(A || B) is the same as (!A) && (!B)
For example:
// Checking if a person is NOT (a student or employed)
boolean isStudent = false;
boolean isEmployed = false;
// These two expressions are equivalent:
boolean result1 = !(isStudent || isEmployed);
boolean result2 = !(isStudent) && !(isEmployed);
System.out.println(result1); // true
System.out.println(result2); // trueChallenge
BeginnerYou're helping a pet shop create a system to determine if they can sell a pet to a customer.
Initialize the following variables:
hasLicensewith the valuetruehasSpacewith the valuefalsehasExperiencewith the valuetrue
Write the following logical expressions to determine if:
canSellRegularPet: Customer needs EITHER a license OR experience, AND must have spacecanSellExoticPet: Customer needs BOTH a license AND experience, AND must have spacecannotSellAnyPet: Customer has NO license AND NO experience, OR has NO spaceresult:canSellRegularPetORcanSellExoticPetORcannotSellAnyPet
Tip: Use the variable values directly in your expressions. For example, hasSpace is false, so "must have space" means you use hasSpace as-is (without !) — the variable already holds the real value. Use ! only when the condition requires the opposite of what the variable holds, such as "has NO license" → !hasLicense.
Cheat sheet
De Morgan's Law helps simplify logical expressions with NOT operators:
!(A && B) is equivalent to (!A) || (!B)
!(A || B) is equivalent to (!A) && (!B)
Example with AND:
int number = 15;
boolean result1 = !(number >= 1 && number <= 10);
boolean result2 = !(number >= 1) || !(number <= 10);
// Both expressions are equivalentExample with OR:
boolean isStudent = false;
boolean isEmployed = false;
boolean result1 = !(isStudent || isEmployed);
boolean result2 = !(isStudent) && !(isEmployed);
// Both expressions are equivalentTry it yourself
public class Main {
public static void main(String[] args) {
// Initialize variables
// Calculate conditions
boolean canSellRegularPet =
boolean canSellExoticPet =
boolean cannotSellAnyPet =
boolean result =
// Don't delete the lines below
System.out.println("Can sell regular pet: " + canSellRegularPet);
System.out.println("Can sell exotic pet: " + canSellExoticPet);
System.out.println("Cannot sell any pet: " + cannotSellAnyPet);
System.out.println("Result: " + result);
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorIncrement/DecrementPost Increment/DecrementArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 43Variables Part 2
ConstantsNaming ConventionsRecap - Initialize VariablesType Casting Part 1Type Casting Part 26Decision Making
If StatementIf - ElseSwitch StatementTernary OperatorRecap - If ElseNested If - Else