Comparison & Logical Operators
Comparison operators compare two values and return true/false. Logical operators combine multiple conditions. Together, they form the foundation for decision-making in your programs. This is a foundational concept in enterprise application development that professional developers rely on daily. The explanations below are written to be beginner-friendly while covering the depth and nuance that comes from real-world Java experience. Take your time with each section and practice the examples
40 min•By Priygop Team•Last updated: Feb 2026
Comparison & Logical Operators
Example
public class ComparisonLogical {
public static void main(String[] args) {
int age = 20;
double gpa = 3.8;
// Comparison Operators (return boolean)
System.out.println("=== Comparison ===");
System.out.println("age == 20: " + (age == 20)); // Equal to
System.out.println("age != 18: " + (age != 18)); // Not equal
System.out.println("age > 18: " + (age > 18)); // Greater than
System.out.println("age < 25: " + (age < 25)); // Less than
System.out.println("age >= 20: " + (age >= 20)); // Greater or equal
System.out.println("gpa <= 4.0: " + (gpa <= 4.0)); // Less or equal
// Logical Operators (combine conditions)
System.out.println("\n=== Logical ===");
boolean isAdult = age >= 18;
boolean hasGoodGrades = gpa >= 3.5;
// AND (&&) — both must be true
System.out.println("Adult AND good grades: " + (isAdult && hasGoodGrades));
// OR (||) — at least one must be true
System.out.println("Adult OR good grades: " + (isAdult || hasGoodGrades));
// NOT (!) — reverses the boolean
System.out.println("NOT adult: " + (!isAdult));
// Practical example
boolean eligible = (age >= 18) && (gpa >= 3.0);
System.out.println("\nScholarship eligible: " + eligible);
}
}Try It Yourself: Eligibility Checker
Try It Yourself: Eligibility CheckerJava
Java Editor
✓ ValidTab = 2 spaces
Java|23 lines|905 chars|✓ Valid syntax
UTF-8