Introduction to Variables & Data Types
Learning Content
Last updated: July 21, 2026
Introduction to Variables & Data Types
Every Apex program works with data. Whether you're storing a customer's name, calculating a discount, tracking an Opportunity amount, or updating an Account record, Salesforce needs a place to temporarily hold that information while your code executes.
This temporary storage is called a Variable. Every variable must be associated with a Data Type, which tells Apex what kind of value the variable can store and what operations can be performed on it.
Since Apex is a strongly typed programming language, every variable must declare its data type before it can be used. This enables Salesforce to detect errors early, optimize memory usage, and maintain data integrity.
💡 LearnFrenzy Insight
Imagine a modern warehouse.
Every storage box has a label:
- 📦 Electronics
- 📦 Clothing
- 📦 Documents
- 📦 Food
You cannot store food inside the electronics box. Similarly, every Apex variable has a predefined data type. An Integer variable stores whole numbers, a String stores text, a Date stores calendar dates, and so on. The data type acts as the label that tells Salesforce what kind of information belongs in that variable.
What is a Variable?
A Variable is a named memory location that temporarily stores information while an Apex program executes.
Instead of repeatedly writing the same value throughout your code, you store it inside a variable and reuse it whenever needed.
String customerName = 'Rahul Sharma'; System.debug(customerName);
In this example:
- String → Data Type
- customerName → Variable Name
- 'Rahul Sharma' → Stored Value
What is a Data Type?
A Data Type defines the kind of information a variable can hold and determines how Salesforce stores and processes that data.
| Data Type | Stores | Example |
|---|---|---|
| Integer | Whole Numbers | 100 |
| Decimal | Currency / Precise Numbers | 1999.99 |
| Boolean | True / False | true |
| String | Text | 'LearnFrenzy' |
| Date | Calendar Date | 2026-07-19 |
| DateTime | Date & Time | 2026-07-19 10:30 AM |
| Id | Salesforce Record ID | 001XXXXXXXXXXXX |
Variable Declaration Syntax
DataType variableName = value;
Example:
Integer age = 25; Boolean isActive = true; String company = 'LearnFrenzy';
Variable Declaration Breakdown
| Part | Description |
|---|---|
| Data Type | Specifies the type of data that will be stored. |
| Variable Name | Name used to reference the value. |
| = | Assignment operator. |
| Value | Initial value assigned to the variable. |
| ; | Statement terminator. |
How Variables Work Internally
Variable Declaration
│
▼
Salesforce Allocates Memory
│
▼
Stores Value
│
▼
Program Uses Variable
│
▼
Value Can Change
│
▼
Memory Released
(After Execution Ends)
Common Variable Naming Conventions
Writing clean, readable code is a hallmark of a good developer. Following these conventions will help you and your team understand the code better.
| Best Practice | Good Example | Poor Example |
|---|---|---|
| Use meaningful names | customerName | cn or x |
| Use camelCase | totalAmount | total_amount or TotalAmount |
| Avoid abbreviations | accountBalance | acctBal |
| Make names descriptive | isOpportunityClosed | closed |
Real Business Scenario
🏢 Business Example
// Capture order details
String customerName = 'Rahul Sharma'; // Text
Decimal orderAmount = 15000.75; // Precise number with decimals
Boolean paymentReceived = true; // True/False status
Date orderDate = Date.today(); // System's current date
// Process the order
System.debug('Order for: ' + customerName);
System.debug('Total Amount: $' + orderAmount);
System.debug('Payment Status: ' + paymentReceived);
During order processing:
- Customer name is stored in a String variable.
- Order amount is stored in a Decimal variable.
- Payment status is stored in a Boolean variable.
- Order date is stored in a Date variable.
Using appropriate data types makes the code easier to understand and reduces errors.
Why Strongly Typed Variables Matter
Apex is a strongly typed language. This means the data type of a variable is known at compile time. This strictness provides several critical benefits:
| Benefit | Description |
|---|---|
| Compile-Time Checking | Detects invalid assignments before execution. |
| Improved Performance | Optimizes memory allocation. |
| Better Readability | Developers immediately understand the stored value. |
| Data Integrity | Prevents incompatible values from being stored. |
Primitive vs Special Data Types
As you progress, you'll see that data types in Apex are broadly divided into two categories:
| Primitive Types (Simple Values) | Special Types (Complex Values) |
|---|---|
| Integer, Decimal, Double, Long, Boolean, String, Date, DateTime, Time | Id, Blob, Object, Enum, Collections (List, Set, Map), SObject |
| Purpose: Store a single, indivisible piece of data. | Purpose: Store more complex data, like a record ID or a collection of items. |
Don't worry about Special Types right now; we'll explore them in detail in future chapters.
🔍 Behind the Scenes: What Happens When You Declare a Variable?
Developer Writes
Integer score = 90;
│
▼
Compiler Checks
✔ Integer Exists?
✔ Variable Name Valid?
✔ Value Compatible?
│
▼
Memory Allocated
│
▼
Value Stored
│
▼
Variable Ready to Use
✅ Best Practices
- Use meaningful variable names.
- Choose the correct data type for the stored value.
- Initialize variables whenever possible.
- Follow camelCase naming conventions.
- Avoid using generic names such as data, temp, or value unless appropriate.
⚠️ Common Mistakes
- Using the wrong data type for a value.
- Poor variable names like a, x, temp1.
- Forgetting to initialize variables when required.
- Using String instead of Id for Salesforce record IDs.
🎯 Interview Tip
Question: Why does Apex require every variable to have a declared data type?
Answer: Apex is a strongly typed language. Declaring a data type enables compile-time validation, improves performance, enforces data integrity, and makes code easier to read and maintain.
- ✔ A variable stores data temporarily during program execution.
- ✔ Every variable must have a data type.
- ✔ Apex is a strongly typed language.
- ✔ Data types determine what values a variable can store.
- ✔ Use meaningful variable names and the correct data type.
- ✔ Variables are the foundation of every Apex program.
➡ Next Lesson
In the next lesson, you'll explore the Integer data type, learn how Apex stores whole numbers, understand its range, discover common arithmetic operations, and apply it to real-world Salesforce business scenarios.
Practice What You've Learned
Test your understanding with these practice exercises