What Are Operators?
Operators are the little symbols that let you assign values, do maths, and compare things. You’ve been using them since episode 8 without me ever explaining them, so let’s put that right.
Salesforce publishes a full list of expression operators and I’d encourage you to skim it at some point. But I’m not going to march you through all of them here, because honestly a decent chunk of that list you will never type in your entire career. Bitwise shift operators are on there. Have I ever used one in Apex? Reader, I have not.
So let’s do the ones you’ll actually use every single day.
Assignment
Integer x = 5; //assignment. x is now 5.
x += 3; //addition assignment. x is now 8.
x -= 2; //subtraction assignment. x is now 6.
x *= 2; //multiplication assignment. x is now 12.
x /= 4; //division assignment. x is now 3.Those compound ones are pure shorthand. x += 3 is identical to x = x + 3, just shorter and easier to read once you’re used to it.
The + operator also glues Strings together, which you’ll use constantly in debug statements:
String name = 'Matt';
System.debug('Hello ' + name + '!'); //Hello Matt!= vs == (Read This Bit Twice)
Right. THIS is the one. This is the operator mistake that everybody makes, that I still occasionally make, and that will have you glaring at perfectly innocent code for twenty minutes.
One equals sign assigns. Two equals signs compare.
Integer x = 5; //x now holds 5
Boolean isFive = (x == 5); //asks a question. isFive is true.Say that out loud a few times. Single equals: “make this that.” Double equals: “is this that?”
Thankfully Apex is a lot stricter about this than some languages, so if (x = 5) won’t even compile. That’s the compiler doing you a favour, so try not to resent it.
The full set of comparison operators:
x == y //equal to
x != y //not equal to
x < y //less than
x > y //greater than
x <= y //less than or equal to
x >= y //greater than or equal toComparing Strings Is Case Insensitive
Here’s a genuine Apex quirk that surprises people coming from other languages:
String one = 'TACO';
String two = 'taco';
System.debug(one == two); //true. Yes, really.In Apex, == on Strings ignores case. In Java it very much does not. If you actually need a case sensitive comparison, use equals():
System.debug(one.equals(two)); //falseWorth filing away. It’s helpful about 90% of the time and quietly ruins your day the other 10%.
Logical Operators
For stringing conditions together:
Boolean isCool = true;
Boolean isSmart = false;
isCool && isSmart //AND - both must be true. false here.
isCool || isSmart //OR - either can be true. true here.
!isCool //NOT - flips it. false here.One genuinely useful detail: Apex short circuits these. With &&, if the left side is false it never even looks at the right side, because the answer can’t change. Same with || when the left side is true.
That’s not trivia, it’s a technique. It’s why this is safe:
if (myList != null && !myList.isEmpty())
{
//If myList is null, the second check never runs,
//so we never blow up trying to call isEmpty() on nothing.
}Get in the habit of null-checking on the left. It’s free insurance.
Two Operators That’ll Save You Loads Of Typing
The ternary operator is a whole if/else squeezed onto one line:
String result = (x > 5) ? 'Big' : 'Small';Read it as: is x greater than 5? If yes, ‘Big’. If no, ‘Small’. Great for simple either/or assignments. Please do not nest three of them inside each other, though. I’ve seen it. I’ve had to read it. Don’t.
And the safe navigation operator, which is the best thing Salesforce has added to Apex in years:
String acctName = myAccount?.Name;That ?. checks for null before reaching for Name. If myAccount is null you get null back instead of an exception. It erases an enormous amount of defensive null checking, and I wrote a whole post about how much I love it.
What’s Next
If you take one thing away: = assigns, == compares, and String comparison with == ignores case.
So far every variable we’ve made holds exactly one value, which gets limiting fast. In the next episode we start on collections, where one variable holds many.
See you next time!
Get Coding With The Force Merch!!
We now have a redbubble store setup so you can buy cool Coding With The Force merchandise! Please check it out! Every purchase goes to supporting the blog and YouTube channel.
Get Shirts Here!
Get Cups, Artwork, Coffee Cups, Bags, Masks and more here!
Check Out More Coding With The Force Stuff!
If you liked this post make sure to follow us on all our social media outlets to stay as up to date as possible with everything!
Youtube
Patreon
Github
Facebook
Twitter
Instagram
Salesforce Development Books I Recommend
Advanced Apex Programming
Salesforce Lightning Platform Enterprise Architecture
Mastering Salesforce DevOps
Good Non-SF Specific Development Books:
Clean Code
Clean Architecture