Java ProgramsBasics & I/ODemonstrate Logical Operators

Demonstrate Logical Operators in Java

beginner·  Basics & I/O  ·  Operators

Problem

Logical operators (&& || !) combine or invert boolean values to build compound conditions out of simpler ones.

Given two boolean values, print the result of combining them with AND, OR, and NOT.

Input
true, false
Output
true && false = false

Java Program

Java
public class DemonstrateLogicalOperators { public static void main(String[] args) { boolean x = true; boolean y = false; System.out.println(x + " && " + y + " = " + (x && y)); System.out.println(x + " || " + y + " = " + (x || y)); System.out.println("!" + x + " = " + (!x)); System.out.println("!" + y + " = " + (!y)); } }

Output

true && false = false true || false = true !true = false !false = true

Core Logic

Applying AND, OR, and NOT to the same pair of booleans shows how each combines or inverts truth values differently.

How It Works
  1. 1x holds true and y holds false.
  2. 2x && y (AND) is true only when both operands are true — here it's false, since y isn't.
  3. 3x || y (OR) is true when at least one operand is true — here it's true, because of x.
  4. 4!x and !y (NOT) simply flip a single boolean to its opposite value.
For x = true and y = false: x && y is false, x || y is true, !x is false, and !y is true.
💡

Key Point: && and || both short-circuit — if the left operand alone already determines the result, the right operand is never evaluated at all.

Key Concepts

&& operator|| operator! operatorboolean

Approach 2: Java 8

Java
import java.util.function.BiPredicate; import java.util.function.UnaryOperator; public class LogicalOperatorsLambda { public static void main(String[] args) { boolean x = true; boolean y = false; // Each operator is stored as a named, reusable lambda BiPredicate<Boolean, Boolean> and = (p, q) -> p && q; BiPredicate<Boolean, Boolean> or = (p, q) -> p || q; UnaryOperator<Boolean> not = p -> !p; System.out.println(x + " && " + y + " = " + and.test(x, y)); System.out.println(x + " || " + y + " = " + or.test(x, y)); System.out.println("!" + x + " = " + not.apply(x)); System.out.println("!" + y + " = " + not.apply(y)); } }

Output

true && false = false true || false = true !true = false !false = true

Core Logic

Wrapping && and || in named BiPredicates, and ! in a named UnaryOperator, turns each logical operator into a reusable value instead of an inline expression.

How It Works
  1. 1BiPredicate<Boolean, Boolean> and = (p, q) -> p && q; and BiPredicate<Boolean, Boolean> or = (p, q) -> p || q; store the AND and OR logic as lambdas.
  2. 2UnaryOperator<Boolean> not = p -> !p; stores the NOT logic the same way, taking one boolean instead of two.
  3. 3and.test(x, y) and or.test(x, y) call the two-argument lambdas; not.apply(x) calls the one-argument one.
  4. 4Each result is printed with the same label as the direct version.
For x = true and y = false: and.test(x, y) is false, or.test(x, y) is true, not.apply(x) is false, and not.apply(y) is true.
💡

Key Point: BiPredicate<Boolean, Boolean> is the standard functional interface for a two-argument boolean test — Java has no dedicated 'BooleanBinaryOperator', so BiPredicate fills that role.

Key Concepts

BiPredicateUnaryOperatorfunctional interface

Related Programs