Careeroza
Boolean Expressions
Expressions that Return a Boolean
Example
Comparisons and logical operators produce boolean values. You can store them in variables or use them directly.
public class Main {
public static void main(String[] args) {
int marks = 72;
int attendance = 68;
boolean passed = marks >= 40;
boolean eligibleForExam = attendance >= 75;
boolean distinction = marks >= 75;
System.out.println("Passed: " + passed);
System.out.println("Eligible: " + eligibleForExam);
System.out.println("Distinction: " + distinction);
System.out.println("Placement eligible: " + (passed && attendance >= 60));
System.out.println(10 == 15);
System.out.println("abc".equals("abc"));
}
}Output
Passed: true Eligible: false Distinction: false Placement eligible: true false true
Truth Table
| A | B | A && B | A || B | !A | A ^ B (XOR) |
|---|---|---|---|---|---|
| true | true | true | true | false | false |
| true | false | false | true | false | true |
| false | true | false | true | true | true |
| false | false | false | false | true | false |
Exercise: A movie ticket is allowed when age >= 18 OR the person is with a parent (withParent), AND the show is not sold out (soldOut). Write the expression.
Show answer
boolean allowed = (age >= 18 || withParent) && !soldOut;