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.

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!

## 4 Comments