Boolean Expressions
Boolean expressions evaluate to true or false.
Example:
int score = 85;
if(score >= 90){
System.out.println("A");
}
else if(score >= 80){
System.out.println("B");
}
else {
System.out.println("C");
}
The program checks conditions in order and prints the grade based on the score.
Comparison operators:
== (equals), != (not equals), >, <, >=, <=
Logical operators:
&& (AND), || (OR), ! (NOT)
Boolean Order of Operations:
Parentheses ()
NOT !
Relational operators (>, <, >=, <=)
Equality operators (==, !=)
AND &&
OR ||
Example:
int x = 5;
if(x > 3 && x < 10){
System.out.println("Valid");
}
Both conditions must be true for the message to print.
int x = 5;
boolean result = x > 3 && x < 10 || x == 20;
First: x > 3 → true
Then: x < 10 → true
Then: true && true → true
Then: x == 20 → false
Finally: true || false → true
Final result: true
The AND is evaluated before the OR, which is why the expression is grouped that way.
Short-Circuit Evaluation:
Short-circuiting means Java stops evaluating early if it already knows the result.
AND (&&) Short-Circuit
If the first condition is false, Java stops immediately.
Example:
int x = 5;
if(x > 10 && x < 20){
System.out.println("Hello");
}
x > 10 → false
Java stops here
x < 20 is NEVER checked
Since AND requires both sides to be true, one false already makes the whole thing false.
OR (||) Short-Circuit
If the first condition is true, Java stops immediately.
Example:
int x = 5;
if(x < 10 || x > 100){
System.out.println("Hello");
}
x < 10 → true
Java stops here
x > 100 is NEVER checked
Since OR only needs one true, Java doesn’t check the second condition.