Beginner 15 min read

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
  1. Integer leadScore = 85; — the score being evaluated.
  2. Boolean isHotLead = leadScore >= 80; — leadScore >= 80 is a comparison — it evaluates to true or false, which is stored directly in isHotLead.
  3. System.debug(isHotLead); — logs the result: true, since 85 >= 80.

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

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Comparison Operators Quiz

1. What type does a comparison expression like 5 > 3 evaluate to?

2. The = operator can be used to compare two values for equality.

Log in to submit the quiz and save your score.

Leaderboard

  • 1. Monde Masiza 2/2

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.