Skip to main content

Apex Data Types

Apex supports various data types categorized into primitive types, collections, sObjects, and complex types.

Below is a comprehensive list with examples.

1. Primitive Data Types

These store single values such as numbers, text, and dates.

Data Type Description Example
String Sequence of characters String greeting = 'Hello, Salesforce!';
Boolean Stores true or false Boolean isActive = true;
Integer Whole numbers Integer rollNumber = 1001;
Long Large whole numbers Long worldPopulation = 8000000000L;
Double Floating-point numbers Double pi = 3.14159;
Decimal High-precision floating-point numbers Decimal price = 99.99;
ID Salesforce record ID Id recordId = '0015g00000ABCD';
Date Stores date without time Date today = Date.today();
Time Stores time only Time now = Time.newInstance(14, 30, 0, 0);
Datetime Stores both date and time Datetime now = Datetime.now();

DeveloperConsole: anonymous window(Primitive Data Types)

       
        String greeting = 'Hello, Salesforce!';
        Boolean isActive = true;
        Integer rollNumber = 1001;
        Long worldPopulation = 8000000000L;
        Double pi = 3.14159;
        Decimal price = 99.99;
        Id recordId = '0015g00000ABCD';
        Date today = Date.today();
        Time now = Time.newInstance(14, 30, 0, 0);
        Datetime now = Datetime.now();
        
        System.debug(greeting);
        System.debug(isActive);
        System.debug(rollNumber);
        System.debug(worldPopulation);
        System.debug(pi);
        System.debug(price);
        System.debug(recordId);
        System.debug(today);
        System.debug(now);
        System.debug(now);
        
                

2. Collection Data Types

Collections store multiple values in different formats.

Collection Type Description Example
List Ordered collection of elements List<String> names = new List<String>{'Alice', 'Bob'};
Set Unordered collection of unique elements Set<Integer> uniqueNumbers = new Set<Integer>{1, 2, 3};
Map Key-value pairs Map<String, Integer> scoreMap = new Map<String, Integer>{'Alice' => 90, 'Bob' => 85};

DeveloperConsole: anonymous window(Lists, Sets, Maps)

        
        // List Example
List names = new List{'Alice', 'Bob', 'Charlie'};
System.debug(names[0]); // Output: Alice

// Set Example
Set uniqueNumbers = new Set{10, 20, 30};
System.debug(uniqueNumbers.contains(20)); // Output: true

// Map Example
Map ageMap = new Map{'Alice' => 25, 'Bob' => 30};
System.debug(ageMap.get('Alice')); // Output: 25
        

3. sObject Data Types

Used for storing Salesforce object records.

sObject Type Description Example
Standard Object Salesforce standard object Account acc = new Account(Name='ABC Corp');
Custom Object User-defined object MyCustomObject__c obj = new MyCustomObject__c(Name='Test');

DeveloperConsole: anonymous window(sObject)

       
        // Standard Object
Account acc = new Account(Name = 'Tech Corp', Industry = 'IT');
insert acc;
System.debug(acc.Id);

// Custom Object
MyCustomObject__c customObj = new MyCustomObject__c(Name = 'Custom Record');
insert customObj;
System.debug(customObj.Id);
        

4. Enum Data Type

An enumdefines a fixed set of constants.

DeveloperConsole: anonymous window(Enum)

        public enum Status { NEW, IN_PROGRESS, COMPLETED }
        Status currentStatus = Status.NEW;
        System.debug(currentStatus); // Output: NEW
       
        

5. Complex & Special Data Types

Data Type Description Example
Blob Stores binary data Blob imageData = Blob.valueOf('sample data');
Object Generic type Object data = 'Hello';

DeveloperConsole: anonymous window(Blob and Object)

           
       
            // Blob Example
            Blob imageData = Blob.valueOf('sample data');
            System.debug(imageData);

            // Object Example
            Object data = 'Dynamic Type';
            System.debug(data);
       
        

6. Null Values in Apex

- Any uninitialized variable in Apex defaults to `null`.

DeveloperConsole: anonymous window

        String str;
        System.debug(str); // Output: null
    
        Integer number;
        System.debug(number); // Output: null
    
        Boolean flag;
        System.debug(flag); // Output: false
        

Summary of Apex Data Types

Category Data Types
Primitive String, Boolean, Integer, Long, Double, Decimal, ID, Date, Time, Datetime
Collection List, Set, Map
sObject Standard Object, Custom Object
Enum User-defined constants
Complex & Special Blob, Object