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)

 

Primitive Data Types

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

All Apex variables, whether they’re class member variables or method variables, are initialized to null. Make sure that you initialize your variables to appropriate values before using them. For example, initialize a Boolean variable to false.

Apex primitive data types include:

Data Type Description
Blob A collection of binary data stored as a single object. You can convert this data type to String or from String using the toString and valueOf methods, respectively. Blobs can be accepted as Web service arguments, stored in a document (the body of a document is a Blob), or sent as attachments. For more information, see Crypto Class.
Boolean A value that can only be assigned truefalse, or null. For example:

1 Boolean isWinner = true;
Date A value that indicates a particular day. Unlike Datetime values, Date values contain no information about time. Always create date values with a system static method.

You can add or subtract an Integer value from a Date value, returning a Date value. Addition and subtraction of Integer values are the only arithmetic functions that work with Date values. You can’t perform arithmetic functions that include two or more Date values. Instead, use the Date methods.

Datetime A value that indicates a particular day and time, such as a timestamp. Always create datetime values with a system static method.

You can add or subtract an Integer or Double value from a Datetime value, returning a Date value. Addition and subtraction of Integer and Double values are the only arithmetic functions that work with Datetime values. You can’t perform arithmetic functions that include two or more Datetime values. Instead, use the Datetime methods.

Decimal A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type Decimal.

Note

If you do not explicitly set the number of decimal places for a Decimal, the item from which the Decimal is created determines the Decimal’s scale. Scale is a count of decimal places. Use the setScale method to set a Decimal’s scale.

  • If the Decimal is created as part of a query, the scale is based on the scale of the field returned from the query.
  • If the Decimal is created from a String, the scale is the number of characters after the decimal point of the String.
  • If the Decimal is created from a non-decimal number, the number is first converted to a String. Scale is then set using the number of characters after the decimal point.
Double A 64-bit number that includes a decimal point. Doubles have a minimum value of –263 and a maximum value of 263-1. For example:

1 Double d=3.14159;

Scientific notation (e) for Doubles is not supported.

ID Any valid 18-character Lightning Platform record identifier. For example:

1 ID id='00300000003T2PGAA0';

If you set ID to a 15-character value, Apex converts the value to its 18-character representation. All invalid ID values are rejected with a runtime exception.

Integer A 32-bit number that does not include a decimal point. Integers have a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647. For example:

1 Integer i = 1;
Long A 64-bit number that does not include a decimal point. Longs have a minimum value of -263 and a maximum value of 263-1. Use this data type when you need a range of values wider than the range provided by Integer. For example:

1 Long l = 2147483648L;
Object

Any data type that is supported in Apex. Apex supports primitive data types (such as Integer), user-defined custom classes, the sObject generic type, or an sObject specific type (such as Account). All Apex data types inherit from Object.

You can cast an object that represents a more specific data type to its underlying data type. For example:

1 Object obj = 10;
2 // Cast the object to an integer.
3 Integer i = (Integer)obj;
4 System.assertEquals(10, i);

The next example shows how to cast an object to a user-defined type—a custom Apex class named MyApexClass that is predefined in your organization.

1 Object obj = new MyApexClass();
2 // Cast the object to the MyApexClass custom type.
3 MyApexClass mc = (MyApexClass)obj;
4 // Access a method on the user-defined class.
5 mc.someClassMethod();
String Any set of characters surrounded by single quotes. For example,

1 String s = 'The quick brown fox jumped over the lazy dog.';

String size: Strings have no limit on the number of characters they can include. Instead, the heap size limit is used to ensure that your Apex programs don’t grow too large.

Empty Strings and Trailing Whitespace: sObject String field values follow the same rules as in SOAP API: they can never be empty (only null), and they can never include leading and trailing whitespace. These conventions are necessary for database storage.

Conversely, Strings in Apex can be null or empty and can include leading and trailing whitespace, which can be used to construct a message.

The Solution sObject field SolutionNote operates as a special type of String. If you have HTML Solutions enabled, any HTML tags used in this field are verified before the object is created or updated. If invalid HTML is entered, an error is thrown. Any JavaScript used in this field is removed before the object is created or updated. In the following example, when the Solution displays on a detail page, the SolutionNote field has H1 HTML formatting applied to it:

1 trigger t on Solution (before insert) {
2             Trigger.new[0].SolutionNote ='<h1>hello</h1>';
3 }

In the following example, when the Solution displays on a detail page, the SolutionNote field only contains HelloGoodbye:

1 trigger t2 on Solution (before insert) {
2             Trigger.new[0].SolutionNote =
3                          '<javascript>Hello</javascript>Goodbye';
4 }

For more information, see “HTML Solutions Overview” in the Salesforce Help.

Escape Sequences: All Strings in Apex use the same escape sequences as SOQL strings: \b (backspace), \t (tab), \n (line feed), \f (form feed), \r (carriage return), \ (double quote), \ (single quote), and \\ (backslash).

Comparison Operators: Unlike Java, Apex Strings support using the comparison operators ==!=<<=>, and >=. Because Apex uses SOQL comparison semantics, results for Strings are collated according to the context user’s locale and are not case-sensitive. For more information, see Operators.

String Methods: As in Java, Strings can be manipulated with several standard methods. For more information, see String Class.

Apex classes and triggers saved (compiled) using API version 15.0 and higher produce a runtime error if you assign a String value that is too long for the field.

Time A value that indicates a particular time. Always create time values with a system static method. See Time Class.