Data Types in Apex

Data types are used to specify the different sizes and values that can be stored in a variable.

Apex supports the following data types −

  1. Primitive (Integer, Double, Long, Date, Datetime, String, ID, or Boolean)
  2. Enums
  3. sObject
  4. Classes and Interfaces
  5. Collections (Lists, Sets and Maps)

Apex Primitive Data Types

Here, we will discuss the Apex Primitive Data Types supported by Apex.

Boolean & Character Data Types

Boolean: This variable can either be false, true or null. In programming many times, this type of variable can be used as flag to identify if the particular condition is set or not set.

Apex Code Example:

 Boolean b = false;

String: String is basically sequence of char values within single quotes. It does not have any limit for the number of characters. Here, the heap size will be used to determine the number of characters in Apex programming.

Apex Code Example:

String str = ‘Welcome to’

Numerical Data types without Decimal points (i.e., whole numbers)

Integer: It is a 32-bit number without any decimal value . The value range for this data type starts from -2,147,483,648 and the maximum value go up to 2,147,483,647.

Apex Code Example:

Integer i = 5;

Long: This is a 64-bit number without any decimal point. This is used when we need a range of values wider than integer.

Apex Code Example:

Long l = 50;

Numerical Data types with Decimal points (i.e., fractional numbers)

Decimal: A number that includes a decimal point. Decimal is an arbitrary precision number. In Apex Programming, Currency fields are automatically assigned the type Decimal.
And also Decimal has lot of built-in methods and rounding options.

Apex Code Example:

 Decimal dec = 12.34

Double: This is a 64-bit number with a decimal point. This is used when we need a range of values wider than decimal.
Apex Code Example:

 Double  dbl  = 5.1234

Apex Specific Data Types

ID:A valid 18-alphanumeric character record identifier.

Apex Code Example:

 ID  i  = ‘b022w00000EKkeNBBO’

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

Blob: Blob is basically used to store files(e.g., images, emails, documents)

Date: This data type indicates a date. This can only store the date and not the time. For saving the date along with time, we will need to store it in data type of DateTime.

Apex Code Example:

Date  dt  =;

Datetime: This data type indicates a date with time. This can store the date with time.

Apex Code Example:

DataTime  currentDT   =;

Time: This data type indicates a time. This can store only the time.

Apex Code Example:

Time  currentT   =;


  • Null is actually the absence of value so please do not be confused with an empty string or a zero which represents the actual values.

How does all this relate to Salesforce Field Types?

We can equate the Apex data types of field types in Salesforce as follows:

Salesforce Field Type Apex Data Type
Auto-Number String
Checkbox Boolean
Currency By default assign as Decimal
Date Date
Number Integer or Double, depending on if you set the decimal place to zeroor more then zero when you create the field.
Percentage By default assign as Decimal
Phone String
Text, Text(Encrypted), Text Area, Text Area(Long), Text Area(Rich) String
URL String


Enum is an abstract data type that is used to stores one value of a finite set of specified identifiers. You can use Enum keyword to define an Enum. Enum can be used as any other data type in Apex Programming Language.

Example of Enum :

public enum Season { SUMMER, WINTER, SPRING, FALL}


This is a special data type in Salesforce. It is very similar to a table in SQL and contains fields which are similar to columns in SQL. Sobjects are types of either standard or custom objects that stores record data in the database.
For example, Account is a standard sObject and any other user-defined object (like Merchandise object that we created) is a Custom sObject.

Note: Developers refers to SObject and their fields by their API names into the program.

Apex Code Example:

// Declaring a sObject variable of type Standard Object – Account

Account act = new Accont(); = ‘SalesforceDrillers’;
act.description = ‘Best site for learning new technologies’;
System.debug(‘act variable value is =  ’ + act);

// Declaring a sObject variable of type Custom Object – Merchandise__c

Merchandise__c mer = new Merchandise__c(); = ‘Jeans’;
mer.description__c = ‘For Kids’;
System.debug(‘mer variable value is =  ’ + mer);

Subscribe Now