Variables and data types in Salesforce Apex

Variables in Salesforce

Variables in salesforce Apex are the same as the variables in java which acts as a container to store data. But in salesforce we have some extra datatypes which are like sObjects which stores the record of a particular salesforce object.

Data Types in Salesforce

1. Primitive Data Types

Primitive data types in salesforce are “Integer”, “String”, “Double”, “Long”, “Date”, “Datetime”, “Boolean” and one more interesting datatype which is “Id” to store the unique id of the records.

Example (Integer)

Integer noOfTeaCups = 5;
System.debug('No of tea cups = ' + noOfTeaCups);

Here “System.debug()” is used for debugging or printing the value on the developer console in salesforce.

Example (Date)

Date todaysDate =;
System.debug('Today’s date is: '+ todaysDate);

This is how the date variable works.


2. sObject

sObjects are very interesting kind, they store the records of any particular salesforce objects. We just need to specify the object name as the data type, that’s it.

Example (sObject)

Account newAccount = new Account();     //Declaring a sObject
newAccount.Name = 'goCoding ';                //Assigning values
newAccount.Description = 'This is a tutorial website ';
System.debug('newAccount info is --> ' + newAccount);

Try this out in the developer console and the output is:

Variables and data types in Salesforce Apex

3. A collection

A collections is a type of variable which can store multiple data. In salesforce we generally use this to store multiple records. They are of three types list, set and map.

i) List

A list stores multiple data with an ordered index starting from 0 and repeated values are allowed. Lets take a simple example of lists.


List<string> num = new List<string> {'one', 'two', 'three'};
System.debug('numbers --> ' + num);

ii) Set

A set stores multiple data with an unordered index and unique values are allowed. Lets take a simple example of sets.


Set<string> animals = new Set<string>{'lion', 'tiger', 'leopard'};
System.debug('animals --> ' + animals);

iii) Maps

Well, maps are different. It stores the multiple data of key and value pairs. Here each value has a unique key. Both can be of any datatypes. Lets see a simple example.


Map<string, string> RollNoToName = new Map<string, string>
{'1'=>'Marc', '2'=>'Kevin'};
System.debug('Rollno’s to Names --> ' + RollNoToName);

4. Enums

In simple Enums are user defined data types. We can also store predefined values in that data type. Now when you try to store any other value in that variable it shows error.


public class enum week
                SUN, MON, TUE, WED, THUR, FRI, SAT

Now after creating this class, you can declare any variable with this “week” datatype and the values assigning to them should only be one of these “SUN, MON, TUE, WED, THUR, FRI, SAT” like,

week x = week.SUN;

5. Classes

A class is a blueprint for an object which consists of all the methods and variables. Apex classes are similar to java classes.

To create a class in

–  pen developer console

– click file

– click new

– click Apex class

– Enter the name of the class

– click OK, there you go.

6. Objects

Objects are the instance of a class in simple. In salesforce we can also create objects of sObjects which is simple.

7. Interfaces

An Interface is similar to class but the difference is in class we provide the methods body but in Interface we provide only method names but not body we have to implement them to our classes and then we have to write the method body.

8. Null

Null is a reserved word in apex. When you create a variable without initializing anything to it then the variable stores null. Also when you initialize the value with a word null then also the variable stores null. Here, “stores null” means stores nothing or empty.



  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.