Study guide
6.1 Variables, Constants & Apex Data Types
Salesforce Certified Platform Developer I Exam
Track your progress as you read
Finished sections get a check mark.
1 Introduction & Exam Overview
1.1 Current Exam Facts 1.2 Blueprint Weights & Study Strategy
2 Platform Fundamentals
2.1 Multi-Tenant Architecture & Shared Resources 2.2 MVC Mapping on the Lightning Platform 2.3 Declarative vs Programmatic Decisions 2.4 Agentforce for Developers: Use Cases & Limitations
3 Data Modeling & Schema
3.1 Objects, Fields & Schema Design 3.2 Relationship Types & Implications 3.3 Schema Design Impact on Apex 3.4 Data Import & Export for Development
4 Platform Data Features
4.1 Formula Fields & Roll-Up Summaries 4.2 Core CRM Objects & Standard Schema 4.3 AppExchange Extensions & Platform Boundaries
5 Declarative Process Automation
5.1 Record-Triggered Flows & Declarative Automation 5.2 Approval Processes & Business Rules 5.3 When to Use Apex vs Declarative Tools
6 Apex Language Basics
6.1 Variables, Constants & Apex Data Types 6.2 Lists, Sets & Maps 6.3 Control Flow & Exception Handling 6.4 Classes, Interfaces & OOP Patterns
7 SOQL, SOSL & DML
7.1 SOQL Query Fundamentals 7.2 SOSL Search Fundamentals 7.3 DML Operations & Bulkification 7.4 Governor Limits & Transaction Boundaries
8 Apex Triggers & Order of Execution
8.1 Trigger Structure & Context Variables 8.2 Bulk Trigger Patterns & Best Practices 8.3 Save Order of Execution 8.4 Recursion, Cascading & Transaction Control
9 Asynchronous Apex
9.1 @future Methods 9.2 Queueable Apex 9.3 Batch Apex 9.4 Schedulable Apex & Job Orchestration
10 Security, Events & Schema Access
10.1 Sharing, FLS, CRUD & Injection Prevention 10.2 Platform Events Publish & Subscribe 10.3 Programmatic Schema Access (Describe)
11 Visualforce
11.1 Visualforce Pages & Markup 11.2 Standard, Custom Controllers & Extensions 11.3 Visualforce in Lightning Experience
12 Lightning Components
12.1 Lightning Component Framework Benefits 12.2 Aura Component Structure & Resources 12.3 Lightning Web Components Fundamentals 12.4 LWC Data Access: Wire, Imperative & Events
13 UI Composition & Apex Integration Patterns
13.1 Composing UI in Lightning Apps 13.2 UI Security, Performance & Accessibility Patterns 13.3 Apex Actions for Flow, LWC, and Agentforce
14 Apex Testing
14.1 Testing Framework & Coverage Requirements 14.2 Writing Unit Tests for Triggers & Classes 14.3 Test Data Strategies & Isolation 14.4 Executing Tests & Execute Anonymous Differences
15 Debugging & Developer Tools
15.1 Debug Logs & Monitoring 15.2 Developer Console, CLI, VS Code & Workbench 15.3 Sandboxes, Scratch Orgs & Environment Strategy
16 Deployment & Release
16.1 Change Sets & Metadata Deployment 16.2 Unlocked Packages, Metadata API & Salesforce DX 16.3 Release Readiness & Credential Maintenance
6.1 Variables, Constants & Apex Data Types
Key Takeaways Explain This Topic AI
Apex is a strongly typed, Java-like language: every variable has a declared type, and the compiler rejects mismatched assignments
Use final for constants; prefer Decimal over Double for money and precise currency math
Primitive types include Integer, Long, Double, Decimal, Boolean, String, Date, Datetime, Time, Id, and Blob
sObject types (Account, Contact, custom objects) are first-class values you can construct, query, and DML
Uninitialized variables are null; null dereference throws NullPointerException—guard before calling methods on possibly null references
Last updated: August 2026
6.1 Variables, Constants & Apex Data Types
Quick Answer: Apex is a strongly typed , object-oriented language similar to Java. You declare variables with an explicit type, mark unchanging values with final , prefer Decimal for money, and treat null carefully because method calls on null throw NullPointerException . sObject types (standard and custom) are first-class values alongside primitives.
This section is foundation for Process Automation and Logic on Platform Developer I. Later chapters assume you can read type signatures, choose Decimal vs Double, and reason about null and conversion behavior under exam scenarios.
Declaring Variables and Constants
Declare a variable with a type, optional initializer, and a name that starts with a letter or underscore: Integer quantity = 10;
- String status = 'Open';
- Boolean isActive;
- Account acct = new Account(Name = 'Acme');
Uninitialized locals default to null (including numeric types). That is different from some languages that zero-initialize numbers. Writing Integer n; System.debug(n + 1); fails at runtime with a null-related error because you cannot unbox null for arithmetic without a non-null value.
- Use final for values that must not be reassigned after initialization: final Integer MAX_RETRIES = 3;
- final String DEFAULT_STATUS = 'New';
final variables can be assigned once (including constructor or declaration). Attempting a second assignment does not compile. Class-level static final constants are common for limit thresholds, default statuses, and configuration keys that should not drift across methods.
Exam trap: final does not make an sObject’s field values immutable. You can still change acct.Name on a final Account acct reference; you simply cannot point acct at a different Account instance.
Primitive Types You Must Know
- Type Role Exam notes
- Integer 32-bit whole number Overflow wraps; use for counts and small IDs
- Long 64-bit whole number Larger ranges; suffix L in literals ( 100L )
- Double IEEE floating point Approximate; not for currency
- Decimal Arbitrary-precision decimal Money, rates, tax —default for currency fields
- Boolean true / false / null Three-state when uninitialized
- String Unicode text Immutable; use == for value equality
- Date Calendar date (no time) Date.today() , Date.newInstance(y,m,d)
- Datetime Date + time + timezone context Datetime.now() , GMT storage considerations
- Time Time of day only Less common on exam but valid
- Id 15- or 18-character Salesforce ID Case-sensitive 15-char; 18-char is case-insensitive
- Blob Binary data Attachments, callout bodies, Crypto
String equality: Use == for content comparison in Apex (unlike Java’s reference == ). Use === only when you specifically care about reference identity for objects.
Id type: Prefer Id over String for record identifiers so invalid ID formats fail early and APIs stay clear. 15-character IDs are case-sensitive; 18-character IDs add a checksum suffix and are safer across case-folding systems.
- Decimal for Money (Hard Rule)
- Currency and precise arithmetic belong on Decimal , not Double: Decimal unitPrice = 19.99;
- Decimal qty = 3;
- Decimal total = unitPrice * qty; // 59.97 exactly as Decimal math
- // Currency fields on sObjects are Decimal
- Opportunity o = new Opportunity(Amount = 1000.50);
Double is fine for scientific or approximate metrics. Exam scenarios that mention invoices, tax, discounts, or Opportunity Amount almost always expect Decimal (or the field type already is Currency → Decimal in Apex).
Scale and rounding matter when you divide: Decimal share = 100 / 3; // Integer division first if both are Integer!
- Decimal shareSafe = 100.0 / 3; // Better: involve Decimal/Double explicitly
- Decimal precise = Decimal.valueOf(100).divide(3, 2, RoundingMode.HALF_UP);
Mixing Integer division with money is a classic bug: 100 / 3 is Integer 33 , not 33.33 . Cast or use Decimal operands before dividing money.
sObject Types
Every standard and custom object is an Apex type. You can:
- Construct with named parameters: new Contact(LastName = 'Lee', Email = 'lee@example.com')
- Read/write fields with dot notation: c.Email = 'new@example.com'
- Hold query results: Account a = [SELECT Id, Name FROM Account LIMIT 1];
- Use generic SObject when the type is dynamic
Custom objects and fields use __c suffixes: Invoice__c inv = new Invoice__c(Amount__c = 250); .
- Generic sObject access uses get / put : SObject sob = new Account();
- sob.put('Name', 'Generic Name');
- String name = (String)sob.get('Name');
Prefer concrete types when you know the object; use SObject for frameworks, dynamic SOQL, and reusable utilities.
- Type Conversion and Casting
- Apex converts some primitives automatically (widening), but many conversions are explicit: Integer i = 42;
- Long l = i; // widening OK
- String s = String.valueOf(i); // Integer → String
- Integer parsed = Integer.valueOf('42');
- Decimal d = Decimal.valueOf('19.99');
- Id accountId = (Id)'001000000000001AAA';
Casting sObjects downcasts must match the runtime type or you get a runtime exception: SObject sob = [SELECT Id, Name FROM Account LIMIT 1];
Account a = (Account)sob; // OK if sob is Account
valueOf methods throw when the string is not parseable—handle or validate input rather than assuming clean data from users or integrations.
Null Behavior and Safe Navigation
Null is a first-class value. Common patterns:
- Optional fields on queried records may be null
- Maps return null when a key is missing ( map.get(id) )
Method chains like acct.Owner.Name fail if any segment is null String ownerName = acct.Owner?.Name; // safe navigation: null if Owner is null
- if (acct != null && acct.Name != null) {
- // guarded use
- }
Safe navigation ( ?. ) short-circuits to null instead of throwing. It is excellent for display and defensive coding. Do not use it to hide logic errors when a value is required for business rules—fail clearly when data must exist.
Expressions and Operators
Apex supports familiar arithmetic ( + - * / ), comparison ( == != < > <= >= ), logical ( && || ! ), and ternary ( condition ? a : b ) operators. String concatenation uses + . Assignment operators ( += , -= ) work on numbers and strings as expected.
Boolean short-circuiting applies: in a && b() , b() is not called if a is false—useful when b() would null-dereference.
- instanceof checks runtime type: if (sob instanceof Account) {
- Account a = (Account)sob;
- }
- Putting It Together for the Exam
When a question shows a snippet, ask:
What is each variable’s type, including null?
Is money on Decimal or accidentally on Double/Integer division?
Will a cast or valueOf throw on bad input?
Does field access assume a non-null relationship?
Mastering these basics prevents missing “easy” items so you can spend time on triggers, bulkification, and governor limits in later chapters.