Coding/Learn/java/booleans comparison
Java Foundational Course
01.Introduction to Java
02.Your First Java Program
03.Syntax & Code Structure
04.Comments in Java
05.Variables & Declaration
06.Primitive Data Types
07.Numbers & Arithmetic Operators
08.Strings in Java
09.Booleans & Relational Operators
10.Type Casting & Conversion
11.Scanner & Interactive Input
12.Logical Operators (&&, ||, !)
13.If Statements
14.If-Else Statements
15.If-Else-If Ladders
16.Switch Statements
17.While Loops
18.Do-While Loops
19.Standard For Loops
20.Break & Continue Statements
21.Arrays in Java
22.Array Indexing & Traversal
23.Enhanced For-Each Loop
24.Essential String Methods
25.Methods in Java
26.Method Parameters & Arguments
27.Method Overloading
28.Classes & Objects in Java
29.Constructors in Java
30.Exception Handling (try-catch-finally)
31.Capstone Mini Projects
Course Certificate0%

Complete all lessons to earn

LearnBooleans & Relational Operators
9 / 31 0%
Lesson 09 of 31
5 mins read

Booleans & Relational Operators

PreviousNext Lesson
In Plain English

Booleans in Java are primitive values that can only be either `true` or `false`. Relational comparison operators compare two numbers or primitives and evaluate to a boolean result, forming the bedrock of program decision-making.

The Mental Model

"Think of comparison operators as security checkpoint questions. "Is passenger age >= 18?" The answer is strictly a boolean: YES (true) or NO (false)."

Deep Dive: How It Works

Relational operators: `==` (equal to), `!=` (not equal to), `>` (greater than), `<` (less than), `>=` (greater or equal), `<=` (less or equal).

In Java, booleans are distinct from numbers: `1` is NOT equivalent to `true`, and `0` is NOT equivalent to `false` (unlike C/C++ or Python).

Syntax Blueprint

boolean isAdult = age >= 18;
boolean isEqual = x == y;
boolean isDifferent = a != b;

Comparisons evaluate directly into a boolean value true or false.

PointCore Rules to Remember

Point star
Strict Type Separation: Java will not allow you to treat integers as booleans.
Point star
Assignment = vs Comparison ==: = assigns a value; == tests for equality.
Point star
Clean Relational Syntax: Works across all numeric primitive types.

Common Beginner Traps & How to Fix Them

Mistake:Using single = inside comparison: if (x = 5)

Why it happens: Single = assigns value 5 to x instead of testing equality.

How to fix: Always use double == for checking equality: if (x == 5).

Pro Tip: Boolean variables can be named with is/has prefixes (e.g. isOnline, hasPermission) for crystal-clear readability.

Live Interactive Example

Hit Run Code to see it live
Comparing Values in Java
Java 13
1
2
3
4
5
6
7
8
9
10
11
12
Output Console
Click "Run Code" to view the rendered output.
How it works: 75 > 65 evaluates to true; 75 == 65 evaluates to false.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Check passing exam score

  • PointGiven studentScore = 78 and passingScore = 70, check if the student passed.
  • PointDeclare a boolean "hasPassed" and print "Passed: " + hasPassed.
1
2
3
4
5
6
7
8
9
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates