Comparison Operators
By the end of this lesson, you'll be able to:
- Use ==, !=, >, <, >=, and <= correctly
- Explain the difference between == and === in Apex
- Predict the Boolean result of a comparison expression
Prerequisites: "Assignment Operators"
The six comparison operators
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> |
Greater than |
< |
Less than |
>= |
Greater than or equal to |
<= |
Less than or equal to |
Every one of these evaluates to a Boolean — true or false, nothing else.
A note on == vs ===
Apex also has === (strict equality) and !==, mainly relevant for comparing objects by reference rather than by value — an edge case you won't need until much later. For primitives like Integer and String, plain == is what you'll use nearly all the time.
A real business example: Lead Management
Integer leadScore = 85;
Boolean isHotLead = leadScore >= 80;
System.debug(isHotLead); // true
This exact pattern — a comparison producing a Boolean, ready to feed straight into an if statement next module — is how most real business rules start life in Apex.
Common mistakes
- Using = instead of ==.
=assigns;==compares. Apex's compiler catches most misuses of this, but it's worth training your eye to spot the difference immediately.
Exercise
Declare Integer leadScore = 72. Declare a Boolean qualifiesForFollowUp that is true if leadScore is 70 or above. Debug it.
Show hint
Use >= directly: leadScore >= 70
Comparison Operators Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Comparison operators compare two values and always produce a Boolean — true or false. These are what make the decisions in Module "Making Decisions" possible, so getting comfortable with them now pays off immediately next module.