Variable Operations in Apex

Thankfully, there aren’t many variable operations in Apex. The most used operation by far is just “+,” which adds two variables together. We know how to add integers, but how can we add string variables? What about booleans or dates? Let’s find out! 

Integer & Decimal operations

Operations for integer variables don’t come with any nasty surprises because they consist of very basic math. They support simple functions like “+,” “-,” “*” or “/.” They all do just what you expect:

Integer four = 2 + 2;
Integer var2 = 3 * 3;
Integer variable3 = 100 - 99;

The only operator in Apex that doesn’t behave like it does in real life is the division operator:

Integer whatHappensHere = 7 / 6; // What value will this variable have?

If you run this code and output the value of the variable, you will get “1.” That’s obviously wrong. To understand why this happens, you must ask yourself, “Is the type on the left side equal to the type on the right side?” No, it’s not! We declared a variable as an integer but initialized with a decimal value on the right side. To get the correct value, you should use Decimal data types:

Decimal correctVariable = 7.0 / 6.0;

String operations

Unsurprisingly, if we add “Al” to “ex,” we get “Alex!” So here’s how we can add variables in Apex:

String alexName = 'Al' + 'ex';
String al = 'al';
String ex = 'ex';
String againAlex= al + ex;

Even though we can do a lot of things with strings, we simply add them most of the time.

Boolean operations

When it comes to operations, booleans are fascinating. Their operations have roots in math and logic, but we won’t go that crazy here. And I hope that you’re already familiar with them in Formulas. 

There are only three logical operators for boolean variables: “&&,” “||” and “!.” In plain English, they’re called “and,” “or” and “not.”

The result of the operation of two booleans is a boolean. Similarly, the result of any string operation between two strings is always a string.

Boolean isTrue = true || false;
Boolean isFalse = true && false;
Boolean isFalseAgain = !isTrue;
Boolean variable1 = isTrue && isFalseAgain; // false, because the same values as 2. line

Here is the full table of possible boolean combinations.

Variable Operations in Apex
Variable Operations in Apex

The power of booleans is that you can combine them as you want and use them to control the flow of your execution. But we’ll go over that more thoroughly in another blog post. Here’s an example:

Boolean combined = (isOpportunity || isLead || isContact) && (validEmail || validPhone);

Date & Id operations

Just as with strings, you can do a lot of interesting operations with dates, but only by leveraging classes and methods. There are no standard operations like “+” or “-“ for dates. So let’s skip this for now. Much like with Ids, there are some methods, but there is no basic operation.

As you can see, classes unlock many operations and are really powerful in Apex. Therefore, our next topic will be classes and objects!

<h4 class="item-title">admin</h4>


Related Posts

blog images 12

SOQL in Apex

blog images 11

Loops in Apex

blog images 10

Lists in Apex


  • Anna Szabo May 28, 2023

    I absolutely love how each topic on the blog for Apex Fundamentals flows very logically, and I know what to read next, why, where to find it, and what to expect. This content is very unique because it’s written in such an easy-to-comprehend style. Whoever wrote this has the heart of a teacher! Thank you.

    • User Avatar
      admin May 28, 2023

      Thank you, I am glad you find it useful!

  • Praveen M August 16, 2023

    Boolean isTrue = true || false;
    Boolean isFalse = true && false;
    Boolean isFalseAgain = !false;
    Boolean variable1 = isTrue && isFalseAgain; // false, because the same values as 2.

    In the above code, ‘isFalseAgain’ should return true right? As you are declaring it as !false. So variable1 would be true && true which returns true.
    Let me know if I’m missing anything.

    • User Avatar
      admin August 16, 2023

      This is a good point! I updated it 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe for news about new courses or discounts.

No spam, we promise.

    © 2023 All Rights Reserved by site