A data type is like a specialized container or box that can only hold specific kinds of values. Just as you wouldn’t put a basketball in a shoebox, different types of data need different types of containers.
🚀 Complete Apex Data Types Guide
Master Salesforce Apex Data Types with Interactive Visualizations
What is a Data Type?
Understanding data types through visual containers and interactive examples
A data type is like a specialized container or box that can only hold specific kinds of values. Just as you wouldn’t put a basketball in a shoebox, different types of data need different types of containers.
🎮 Try dropping these values into the right containers:
💾 Memory Layout Visualization
Each data type uses different amounts of memory – like different sized storage containers
Shipping Boxes
Small items go in small boxes, large items need big boxes. You can’t fit a refrigerator in an envelope!
Room Types
Kitchen is for cooking, bedroom for sleeping, bathroom for hygiene. Each room has a specific purpose!
Tool Box
Screwdriver for screws, hammer for nails, wrench for bolts. Right tool for the right job!
Primitive Data Types
Fundamental building blocks of Apex programming with detailed specifications and interactive examples
- Most efficient for simple counting and indexing
- Use for loop counters and array indices
- Avoid overflow – check bounds for calculations
- Prefer Integer over Long when range suffices
- Essential for timestamp operations
- Use for large mathematical calculations
- Required when Integer range is insufficient
- Add ‘L’ suffix for long literals
- Strings are immutable – concatenation creates new objects
- Use StringBuilder pattern for multiple concatenations
- String.format() is more readable than concatenation
- Consider String.join() for array/list joining
- Most memory-efficient primitive type
- Use for flags and conditional states
- Boolean expressions are short-circuited
- Default value is false for instance variables
- Always use Decimal for currency and financial data
- More memory intensive than Double but exact
- Use setScale() for consistent decimal places
- Supports arbitrary precision arithmetic
- Use for scientific and engineering calculations
- Avoid for financial calculations (use Decimal)
- Be aware of floating-point precision limits
- More efficient than Decimal for math operations
- Use Date for date-only scenarios (birthdays, due dates)
- More efficient than DateTime when time isn’t needed
- Supports date arithmetic (addDays, addMonths, etc.)
- Always use Date.today() for current date
- Use for timestamps, logging, and scheduling
- Always stored in GMT – timezone conversion for display
- More expensive than Date when time isn’t needed
- Use DateTime.now() for current timestamp
- Use for record references and SOQL queries
- 18-char format is safer for external systems
- Always check for null before using
- Can be used to determine object type from prefix
- Use for binary data (files, images, documents)
- Be mindful of heap size limits (6MB)
- Base64 encoding increases size by ~33%
- Consider streaming for large files
sObject Data Types
Salesforce Objects representing records in the database with comprehensive field structures
📝 sObject Declaration & Usage
🔧 Field Access Methods
🔗 Relationship Navigation
📋 sObject Metadata & Inspection
Collection Data Types
Powerful data structures for handling multiple values with different behaviors and use cases
📋 List<T> – Ordered Collection
🎯 Set<T> – Unique Values Collection
🗺️ Map<K,V> – Key-Value Pairs
| Operation | List | Set | Map |
|---|---|---|---|
| Add Element | O(1) | O(1) | O(1) |
| Search/Contains | O(n) | O(1) | O(1) |
| Remove Element | O(n) | O(1) | O(1) |
| Access by Index | O(1) | N/A | N/A |
Comparison & Best Practices
Comprehensive comparison tables and expert recommendations for choosing the right data types
| Data Type | Memory Size | Range/Capacity | Primary Use Cases | Performance | Example |
|---|---|---|---|---|---|
| Integer | 4 bytes | -2³¹ to 2³¹-1 | Counters, loop indices, quantities | ⚡ Fastest | Integer count = 100; |
| Long | 8 bytes | -2⁶³ to 2⁶³-1 | Timestamps, large numbers | ⚡ Very Fast | Long timestamp = System.currentTimeMillis(); |
| Double | 8 bytes | ±1.7E±308 (15-17 digits) | Scientific calculations, math | ⚡ Fast | Double pi = Math.PI; |
| Decimal | Variable | 28-29 significant digits | Currency, financial calculations | 🐌 Slower but precise | Decimal price = 199.99; |
| String | 2 bytes/char | ~6M characters | Text, names, descriptions | 🐌 Memory intensive | String name = ‘John Doe’; |
| Boolean | 1 byte | true or false | Flags, conditional logic | ⚡ Fastest | Boolean isActive = true; |
| ID | 15-18 bytes | Salesforce records only | Record references, SOQL | ⚡ Fast | ID accountId = ‘001D000001234567’; |
| Date | 8 bytes | 1700-4000 (approx) | Birthdays, due dates | ⚡ Fast | Date birthday = Date.newInstance(1990, 5, 15); |
| DateTime | 16 bytes | Date + Time + Timezone | Timestamps, logging, scheduling | ⚡ Fast | DateTime now = DateTime.now(); |
| Blob | Variable | 6MB (heap limit) | Binary data, files, images | 🐌 Memory intensive | Blob data = Blob.valueOf(‘Hello’); |
| List<T> | Variable | Limited by heap | Ordered data, indexed access | ⚡ Fast access, 🐌 search | List<String> items = new List<String>(); |
| Set<T> | Variable | Limited by heap | Unique values, fast lookup | ⚡ Very fast lookup | Set<String> unique = new Set<String>(); |
| Map<K,V> | Variable | Limited by heap | Key-value relationships | ⚡ Very fast lookup | Map<String, String> data = new Map<String, String>(); |
- ✓ Use Decimal for all financial calculations
- ✓ Always check for null before operations
- ✓ Use Set for uniqueness requirements
- ✓ Pre-size collections when size is known
- ✓ Use String.join() for multiple concatenations
- ✓ Choose smallest appropriate numeric type
- ✓ Use Map for fast key-based lookups
- ✗ Don’t use Double for currency calculations
- ✗ Don’t concatenate strings in loops
- ✗ Don’t ignore potential overflow scenarios
- ✗ Don’t use List.contains() for large datasets
- ✗ Don’t assume collections are never null
- ✗ Don’t use inappropriate types for size/performance
- ✗ Don’t forget about heap size limits
