Skip to main content

SF-0266 · Concept · Easy

What are the different data types we have in salesforce?

✓ Verified by Vikas Singhal · Last reviewed 5/17/2026 · Updated for Spring '26

Apex has four built-in categories of data types — Primitives, Collections, sObjects, and Enums — plus the user-defined types you create yourself (classes and interfaces). Knowing the categories helps you answer everything from “how do I store a list?” to “what’s the difference between Account and SObject?“

1. Primitives

The atomic types. Each one represents a single value, not a collection.

TypePurposeExample
Booleantrue/falseBoolean done = true;
Integer32-bit whole numberInteger count = 42;
Long64-bit whole numberLong big = 9_000_000_000L;
DecimalHigh-precision number for currency, percentDecimal price = 19.95;
Double64-bit floating pointDouble pi = 3.14159;
StringUTF-16 text, immutableString name = 'Acme';
DateCalendar date, no timeDate today = Date.today();
TimeTime of day, no dateTime t = Time.newInstance(9, 0, 0, 0);
DatetimeDate + time, in UTC by defaultDatetime now = Datetime.now();
IdSalesforce 15- or 18-char record IDId accId = '001...';
BlobBinary data (PDFs, images, callout payloads)Blob b = Blob.valueOf('hi');
ObjectThe base type for everythingObject o = 'anything';

Id is technically a strongly-typed String — same wire format, but the compiler validates length and shape.

2. Collections

Containers for multiple values. Three flavors:

List

Ordered, allows duplicates, integer-indexed.

List<String> names = new List<String>{ 'Acme', 'Beta', 'Gamma' };
List<Account> accs = [SELECT Id FROM Account]; // SOQL returns a List

Set

Unordered, no duplicates, fast membership check.

Set<Id> ids = new Set<Id>{ a.Id, b.Id, c.Id };
if (ids.contains(someId)) { ... }

Map

Key-value pairs, no duplicate keys.

Map<Id, Account> byId = new Map<Id, Account>([SELECT Id, Name FROM Account]);
Account a = byId.get(someId);

All three are generic — you specify the element type in <...>.

3. sObjects

Every Salesforce record has an Apex type corresponding to its object: Account, Contact, Opportunity, MyCustomObject__c. They share a common base type, SObject, which lets you write code generic across objects.

Account a = new Account(Name = 'Acme');
SObject anything = a; // upcast to the generic
String name = (String) anything.get('Name');

The generic SObject is how dynamic Apex works.

4. Enums

Named, finite sets of values.

public enum Season { Winter, Spring, Summer, Fall }

Season current = Season.Spring;
if (current == Season.Spring) { ... }

Apex provides several built-in enums you’ll encounter: System.AccessLevel, TriggerOperation, Schema.DisplayType, LoggingLevel. Enums are not interchangeable with Integers — they’re their own type.

5. User-defined classes and interfaces

When the built-in types aren’t enough, you define your own. A wrapper class to bundle related fields for an LWC. A service class to encapsulate business logic. An interface to enforce a contract.

public class OrderDTO {
    @AuraEnabled public String orderNumber;
    @AuraEnabled public Decimal total;
    @AuraEnabled public List<Contact> contacts;
}

A side note on Object

Object is the root of the Apex type hierarchy — every type inherits from it. It’s used when truly anything could be the value (generic deserialization, untyped maps from JSON). Casting Object back to a specific type is your responsibility.

Map<String, Object> payload = (Map<String, Object>) JSON.deserializeUntyped(body);
String customer = (String) payload.get('customer');

What interviewers are really looking for

The naming check is “four categories — primitives, collections, sObjects, enums.” Distinguishing Decimal vs Double (Decimal is precise for currency; Double is binary floating-point and can introduce rounding errors) shows fundamentals. Calling out SObject as the dynamic-Apex base type and user-defined classes as the fifth category that everything else builds on shows architecture-level understanding.

Verified against: Apex Developer Guide — Data Types. Last reviewed 2026-05-17.