Data Types
5.1 Primitive types
32-bit signed integer Integer n = 42; 64-bit signed integer Long big = 9000000000L; 64-bit floating point Double d = 3.14159;
Arbitrary-precision decimal (currency fields use it) Decimal price = 19.99; true, false, or null Boolean active = true; Unicode text in single quotes String s = 'Apex'; Calendar date (no time) Date d = Date.today(); Date + time (stored in GMT) Datetime now = Datetime.now();
Time t = Time.newInstance(9, 30, 0, 0); 15- or 18-character Salesforce record ID Id accId = '001000000000001AAA'; Blob b = Blob.valueOf('text'); Supertype of all types Object o = 5; Important rules:
All variables default to null, including Integer and Boolean — always initialize.
Strings use single quotes: 'Hello'. Double quotes are a compile error.
Integer arithmetic truncates: 7 / 2 is 3. Use Decimal for money: 7.0 / 2 is 3.5.
Integer overflow wraps silently (2147483647 + 1 → -2147483648); use Long for large values.
5.2 Type conversion
Integer i = Integer.valueOf('42');
Decimal d = Decimal.valueOf('19.99');
String s = String.valueOf(3.5);
Long l = i; // implicit widening: Integer → Long → Double → Decimal
Integer back = (Integer) l; // explicit cast (narrowing)
Date dt = Date.valueOf('2025-12-31');
Boolean flag = Boolean.valueOf('true');
Id recordId = Id.valueOf('001000000000001AAA');
Object o = 'text';
String str = (String) o; // TypeException at runtime if o is not a String
Boolean isStr = o instanceof String;5.3 sObjects
An sObject is an in-memory representation of a Salesforce record. Every standard and custom object has a corresponding type:
Naming conventions:
Custom object or custom field Custom relationship name (used in SOQL traversal) Custom metadata type Sharing object for a custom object Field history for a custom object
Change Data Capture event (AccountChangeEvent, Invoice__ChangeEvent)
Relationship fields can be navigated after a query:
Account a = new Account();
a.Name = 'Acme';
a.AnnualRevenue = 5000000;
Contact c = new Contact(FirstName = 'Ada', LastName = 'Lovelace', Email = '[email protected]');
Invoice__c inv = new Invoice__c(Amount__c = 250, Status__c = 'Draft'); // custom object
// generic sObject — type decided at runtime
SObject generic = new Account(Name = 'Generic');
String nameValue = (String) generic.get('Name');
generic.put('Description', 'Created generically');
Schema.SObjectType t = generic.getSObjectType(); // Account
SObject fromType = Schema.getGlobalDescribe().get('Contact').newSObject();Contact con = [SELECT Id, Account.Name, Account.Owner.Email FROM Contact LIMIT 1];
System.debug(con.Account.Name);5.4 Enums
Season.valueOf('XYZ') throws an exception for unknown names; wrap it in try/catch when parsing user input. Built-in enums include System.TriggerOperation, System.LoggingLevel, System.RoundingMode, System.AccessLevel, and System.Quiddity.
public enum Season { WINTER, SPRING, SUMMER, FALL }
Season s = Season.SUMMER;
System.debug(s.name()); // 'SUMMER'
System.debug(s.ordinal()); // 2
List<Season> all = Season.values();
Season parsed = Season.valueOf('FALL'); // String → enum5.5 The Id type
Never hard-code record IDs in code — they differ between orgs and sandboxes.
Id accId = [SELECT Id FROM Account LIMIT 1].Id;
Schema.SObjectType objType = accId.getSObjectType(); // Account
String prefix = String.valueOf(accId).substring(0, 3); // '001'
Id fifteen = '001000000000001'; // 15-char IDs are converted to 18