Data Types and Variables

Apex uses data types, variables, and related language constructs such as enums, constants, expressions, operators, and assignment statements.

  • Data Types

    In Apex, all variables and expressions have a data type, such as sObject, primitive, or enum.

  • Primitive Data Types

    Apex uses the same primitive data types as SOAP API, except for higher-precision Decimal type in certain cases.

  • Collections

    Collections in Apex can be lists, sets, or maps.

  • Enums

    An enum is an abstract data type with values that each take on exactly one of a finite set of identifiers that you specify. Enums are typically used to define a set of possible values that don’t otherwise have a numerical order. Typical examples include the suit of a card, or a particular season of the year.

  • Variables

    Local variables are declared with Java-style syntax.

  • Constants

    Apex constants are variables whose values don’t change after being initialized once. Constants can be defined using the final keyword.

  • Expressions and Operators

    An expression is a construct made up of variables, operators, and method invocations that evaluates to a single value.

  • Assignment Statements

    An assignment statement is any statement that places a value into a variable.

  • Rules of Conversion

    In general, Apex requires you to explicitly convert one data type to another. For example, a variable of the Integer data type cannot be implicitly converted to a String. You must use the string.format method. However, a few data types can be implicitly converted, without using a method.