Basics

Apex Operators

Apex Operators

Apex operators include arithmetic and comparison with SOQL integration.

Introduction to Apex Operators

Apex operators are symbols that perform operations on one or more operands. These operators are fundamental in manipulating data and controlling the flow of your Apex code. In this post, we'll cover various types of Apex operators, including arithmetic, comparison, and logical operators, and demonstrate how they integrate with SOQL queries.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations such as addition, subtraction, multiplication, and division. Here are the common arithmetic operators in Apex:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder)

Comparison Operators

Comparison operators are used to compare two values. These operators return a Boolean value (true or false) based on the comparison:

  • ==: Equal to
  • !=: Not equal to
  • >: Greater than
  • >=: Greater than or equal to
  • <: Less than
  • <=: Less than or equal to

Logical Operators

Logical operators are used to combine two or more conditions. They are vital in control flow statements. Here are the logical operators available in Apex:

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

Using Operators with SOQL

Operators are integral to forming SOQL queries in Apex. They help filter records by comparing field values. Here's a simple example:

In this example, the SOQL query uses the = operator to filter accounts where the industry is 'Technology' and the billing city is 'San Francisco'. Logical operators like AND are used to combine multiple conditions.

Conclusion

Understanding operators in Apex is crucial for building effective and efficient code. Whether you're performing calculations or constructing queries, these operators are the building blocks of your Apex logic. In the next post, we'll delve into conditional statements with if and else constructs.

Previous
Data Types