Note: This release is in preview. Features described here don’t become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can’t guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.

Data Types

In Apex, all variables and expressions have a data type, such as sObject, primitive, or enum.
  • A primitive, such as an Integer, Double, Long, Date, Datetime, String, ID, or Boolean (see Primitive Data Types)
  • An sObject, either as a generic sObject or as a specific sObject, such as an Account, Contact, or MyCustomObject__c (see Working with sObjects in Chapter 4.)
  • A collection, including:
    • A list (or array) of primitives, sObjects, user defined objects, objects created from Apex classes, or collections (see Lists)
    • A set of primitives (see Sets)
    • A map from a primitive to a primitive, sObject, or collection (see Maps)
  • A typed list of values, also known as an enum (see Enums)
  • Objects created from user-defined Apex classes (see Classes, Objects, and Interfaces)
  • Objects created from system supplied Apex classes
  • Null (for the null constant, which can be assigned to any variable)

Methods can return values of any of the listed types, or return no value and be of type Void.

Type checking is strictly enforced at compile time. For example, the parser generates an error if an object field of type Integer is assigned a value of type String. However, all compile-time exceptions are returned as specific fault codes, with the line number and column of the error. For more information, see Debugging Apex.