Salesforce Apex Data Types with Examples

By | December 27, 2022

What type of data is going to store in variable it’s called data types. The apex language is strongly typed so every variable in apex will be declared with the specific data type.

What is Apex Data Types?

Data types will specify the following

i) How much memory for a given variable.
ii) What type of data is stored in a variable.
iii) What is the range of the data that is stored in a variable?
iv) Primitive will create by the compiler. Non-primitive will create by the user.

Apex data types are:-

  • Primitive data types
  • Collections (List/Set/Map)
  • sObject (Generic object account, Contact, Customer_c)
  • Enums

Primitive data types in salesforce

Apex supports the following primitive data types: Boolean, Integer, Long, Double, Date, Datetime, String, ID, and Decimal.


Boolean values represent true or false and contain the literals TRUE and FALSE.

Boolean b;
b = true;
System.debug(‘Value of shipmentDispatched: ‘+b);


Integer data types are whole numbers that range from -2,147,483,648 to 2,147,483,647.

Integer i = 1000;
system.debug(‘ Integer value is: ‘+i);


Long data types are whole numbers that range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Long productRevenue = 21474838973344648L;


Double data types are numbers that contain decimal points and range from -1.79769313486231E+308 to -4.94065645841247E-324 for negative values and 4.94065645841247E-324 to 1.79769313486232E+308 for positive values.

Double d = 3.14159;
System.debug(‘D value is:’ + d )


Date data types contain only date information, without time information.

Date deliveryDate =;
System.debug(‘ShipmentDate: ‘+deliveryDate);


Datetime data types contain both date and time information.

Datetime currDateTime =;
System.debug(‘ShipmentDate: ‘+currDateTime);


String data types are text values and can contain any character.

String str = ‘Rahul’;
System.debug(‘My name is:’ + str);


ID data types are used to store Salesforce record ID values and are 18 characters long.

Id myclass = [select Id from Account Limit 1];
System.debug(‘ID:’ +myclass);


Decimal data types are numbers that contain decimal points and range from -10^-130 to 10^125.

Double d = 356.32;
System.debug(‘Double is:’ +d);


Apex sObjects are a special data type in Salesforce that are used to hold data in memory and are closely related to database tables. They are used to represent an object such as an Account, Contact, or Opportunity.

An sObject can either be a standard object or a custom object and each record in the sObject is represented as a single row in the database. Apex sObjects allow developers to access and manipulate data in Salesforce in a secure and efficient way.

Account accountObj = new Account();
accountObj.Name = ‘Rohit’;
System.debug(‘Account name is:’ +accountObj);
insert accountObj;


  • An Enums is an abstract data type whose values consist of a finite set of identifiers.
  • Hiding the implementation prevents any possible misuse of the values of performing arithmetic and so on.
  • To define an Enum, use the ‘enum’ keyword in our declaration and use curly braces to demarcate the list of possible values.

public enum Color {Red, Pink, Blue, yellow}
Color objC =;
System.debug(‘objC value: ‘+objC);